无法从命令行在 Node 上安装车把
Posted
技术标签:
【中文标题】无法从命令行在 Node 上安装车把【英文标题】:Cannot install handlebars on Node from command line 【发布时间】:2015-01-07 08:54:24 【问题描述】:我一直在尝试让车把与节点一起使用。我的书指示我安装这样的车把:npm install --save express3-handlebar
。这引发了错误
npm WARN deprecated express3-handlebars @0.5.2: THIS PACKAGE HAS BEEN RENAMED TO: express-handlebars
所以我尝试了npm install --save express-handlebar
。
当我尝试启动服务器node meadowlark.js
时,命令提示符显示Express started on....
但是当我将 localhost 放入浏览器时,我收到以下信息:
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)
然后我注意到在我的索引文件中我仍然有
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');
所以我将其更改为从第三行中删除 3
。仍然是同样长的错误行。我做错了什么来收到这个错误?安装车把时我不在正确的目录中吗?我已经从我的应用根目录中尝试过了,其中包含我的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');
我正在使用的示例来自 Web Development with Node & Express(Ethan Brown)。
【问题讨论】:
【参考方案1】:我在您的代码中看到的主要问题是您正在创建 app
的两个实例:
var app = express(); // I see this line twice in your code
这意味着您在错误的应用实例上配置了把手,因此是 "No default engine was specified"
。
如果您删除第二个实例,您应该是黄金。这是我将如何重写该代码的示例:
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.');
);
您可以尝试在 require 语句中使用或不使用 3
,但它应该可以工作。
希望对你有帮助!!!
【讨论】:
非常感谢!很抱歉延迟接受!以上是关于无法从命令行在 Node 上安装车把的主要内容,如果未能解决你的问题,请参考以下文章
通过命令行在 Ubuntu(或 Linux)上安装 Anaconda