最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cannot install handlebars on Node from command line - Stack Overflow

programmeradmin3浏览0评论

I've been trying to get handlebars to work with node using. My book has instructed me to install handlebars like this: npm install --save express3-handlebar. That threw an error

npm WARN deprecated express3-handlebars @0.5.2: THIS PACKAGE HAS BEEN RENAMED TO: express-handlebars

so I tried npm install --save express-handlebar.

When I tried to start the server node meadowlark.js, the mand prompt displayed Express started on....

But when I put localhost into the browser I received the following:

Error: No default engine was specified and no extension was provided.
   at new View (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\view.js:48:42)
   at EventEmitter.app.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\application.js:509:12)
   at ServerResponse.res.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\response.js:904:7)
   at require.create.defaultLayout (C:\Users\myUserName\Desktop\project\meadowlark\site\meadowlark.js:20:6)
   at Layer.handle_error (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\layer.js:58:5)
   at trim_prefix (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:269:13)
   at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:238:9
   at Function.proto.process_params (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:313:12)
   at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:229:12
   at Function.match_layer (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:296:3)

Then I noticed in my index file I still had

var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
    .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

So I changed that to remove the 3 from the third line. Still the same long lines of errors. What have I done wrong to receive this error? Am I not in the correct directory when I do the handlebars install? I have tried it from my app root directory, and the file within it that contains my index.js

var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);

app.get('/', function(req, res) {
 res.render('home');
});
app.get('/about', function(req, res) {
 res.render('about');
});
// 404 catch-all handler (middleware)
app.use(function(req, res, next){
 res.status(404);
 res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next){
 console.error(err.stack);
 res.status(500);
 res.render('500');
});

app.listen(app.get('port'), function() {
    console.log('Express started on http://localhost:' +
        app.get('port') + '; press Ctrl-C to terminate.');
});

var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
    .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

The example I'm working with is from Web Development with Node & Express (Ethan Brown).

I've been trying to get handlebars to work with node using. My book has instructed me to install handlebars like this: npm install --save express3-handlebar. That threw an error

npm WARN deprecated express3-handlebars @0.5.2: THIS PACKAGE HAS BEEN RENAMED TO: express-handlebars

so I tried npm install --save express-handlebar.

When I tried to start the server node meadowlark.js, the mand prompt displayed Express started on....

But when I put localhost into the browser I received the following:

Error: No default engine was specified and no extension was provided.
   at new View (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\view.js:48:42)
   at EventEmitter.app.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\application.js:509:12)
   at ServerResponse.res.render (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\response.js:904:7)
   at require.create.defaultLayout (C:\Users\myUserName\Desktop\project\meadowlark\site\meadowlark.js:20:6)
   at Layer.handle_error (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\layer.js:58:5)
   at trim_prefix (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:269:13)
   at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:238:9
   at Function.proto.process_params (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:313:12)
   at C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:229:12
   at Function.match_layer (C:\Users\myUserName\Desktop\project\meadowlark\site\node_modules\express\lib\router\index.js:296:3)

Then I noticed in my index file I still had

var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
    .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

So I changed that to remove the 3 from the third line. Still the same long lines of errors. What have I done wrong to receive this error? Am I not in the correct directory when I do the handlebars install? I have tried it from my app root directory, and the file within it that contains my index.js

var express = require('express');
var app = express();
app.set('port', process.env.PORT || 3000);

app.get('/', function(req, res) {
 res.render('home');
});
app.get('/about', function(req, res) {
 res.render('about');
});
// 404 catch-all handler (middleware)
app.use(function(req, res, next){
 res.status(404);
 res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next){
 console.error(err.stack);
 res.status(500);
 res.render('500');
});

app.listen(app.get('port'), function() {
    console.log('Express started on http://localhost:' +
        app.get('port') + '; press Ctrl-C to terminate.');
});

var app = express();
// set up handlebars view engine
var handlebars = require('express3-handlebars')
    .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

The example I'm working with is from Web Development with Node & Express (Ethan Brown).

Share Improve this question edited Nov 10, 2014 at 22:34 1252748 asked Nov 10, 2014 at 22:28 12527481252748 15.4k34 gold badges117 silver badges242 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9 +200

The main issue I see with your code is that you are creating two instances of app:

var app = express(); // I see this line twice in your code

This means you are configuring your handlebars on the wrong instance of your app, hence "No default engine was specified".

If you remove the second instantiation you should be golden. Here is an example of how I would rewrite that code:

var express = require('express');
var app = express();
var handlebars = require('express-handlebars')
  .create({ defaultLayout:'main' });
app.engine('handlebars', handlebars.engine);
app.set('view engine', 'handlebars');

app.set('port', 3000);

app.get('/', function(req, res) {
  res.render('home');
});
app.get('/about', function(req, res) {
  res.render('about');
});
// 404 catch-all handler (middleware)
app.use(function(req, res, next){
  res.status(404);
  res.render('404');
});
// 500 error handler (middleware)
app.use(function(err, req, res, next){
  console.error(err.stack);
  res.status(500);
  res.render('500');
});

app.listen(app.get('port'), function() {
  console.log('Express started on http://localhost:' +
  app.get('port') + '; press Ctrl-C to terminate.');
});

You can try that with and without the 3 in your require statement, but it should work either way.

Hope this helps!!!

发布评论

评论列表(0)

  1. 暂无评论