Javascript 节点错误:“找不到模块‘路由’”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34247468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node error: "Cannot find module 'routes'"
提问by Nicholas Barry
Update 12/14/15:I see that the next chapter instructs the reader to create the 'require' module, so I'll forge ahead. The book seems to be poorly edited - maybe they rearranged some content and didn't notice the errors those changes made. Per my original question, below, things are definitely out of order. But even if the book has some errors, it's forcing me to do outside reading to understand those errors, and post questions to SO, and that's helping me learn. Thanks for your help, folks!
2015 年 12 月 14 日更新:我看到下一章指示读者创建“require”模块,所以我会继续前进。这本书的编辑似乎很差——也许他们重新安排了一些内容而没有注意到这些更改所造成的错误。根据我最初的问题,下面的事情肯定是出问题了。但即使这本书有一些错误,它也迫使我通过阅读来理解这些错误,并向 SO 提出问题,这有助于我学习。感谢您的帮助,伙计们!
I'm using the "Web Development with MongoDB and Node.js - Second edition" to learn Node and MongoDB (surprising, right?). The book's approach seems to be to rush the reader through the creation of an app, and only partially explain things along the way. So when something goes wrong, I don't understand why it went wrong or how to fix it. Also, I've found a couple of code typos in the book so far, so maybe one of those is causing my problem.
我正在使用“使用 MongoDB 和 Node.js 进行 Web 开发 - 第二版”来学习 Node 和 MongoDB(令人惊讶,对吧?)。这本书的方法似乎是让读者快速完成一个应用程序的创建,并且只是部分地解释沿途的事情。所以当出现问题时,我不明白为什么会出错或如何修复它。另外,到目前为止,我在书中发现了几个代码错别字,所以其中一个可能导致了我的问题。
Right now, I'm trying to run the server.js file the book has had me create, and I'm running into an error. I'm still very new to Node, Express, and full stack generally, so the error could be because of something really simple and obvious that I'm missing. Help!
现在,我正在尝试运行本书让我创建的 server.js 文件,但遇到了错误。我对 Node、Express 和全栈仍然很陌生,所以错误可能是因为我遗漏了一些非常简单和明显的东西。帮助!
My folder structure is: Folder StructureThe Node Modules folder has a bunch of folders, which were created when I ran this command: npm install express morgan body-parser cookie-parser method-override errorhandler express-handlebars --save
我的文件夹结构是: 文件夹结构Node Modules 文件夹有一堆文件夹,是在我运行这个命令时创建的: npm install express morgan body-parser cookie-parser method-override errorhandler express-handlebars --save
So far, I have two files, the contents of which were copied straight from the book: server.js:
到目前为止,我有两个文件,其中的内容是直接从书中复制的:server.js:
var express = require('express'),
config = require('./server/configure'),
app = express();
app.set('port', process.env.PORT || 3300);
app.set('views', __dirname + '/views');
app = config(app);
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen(app.get('port'), function(){
console.log('Server up: http://localhost:' + app.get('port'));
});
And configure.js, inside the /server/ folder. The code for this file, in the book, appeared to have two typos, which I've corrected in this file. (But maybe I missed others, or "corrected" those incorrectly.)
和 configure.js,在 /server/ 文件夹内。书中这个文件的代码似乎有两个错别字,我已经在这个文件中更正了。(但也许我错过了其他人,或者错误地“纠正”了那些。)
var path = require('path'),
routes = require('./routes'),
exphbs = require('express-handlebars'),
express = require('express'),
bodyParser = require('body-parser'),
cookieParser = require('cookie-parser'),
morgan = require('morgan'),
methodOverride = require('method-override'),
errorHandler = require('errorhandler');
module.exports = function(app){
app.use(morgan('dev'));
app.use(bodyParser.urlencoded({'extended': true}));
app.use(bodyParser.json());
app.use(methodOverride());
app.use(cookieParser('some-secret-value-here'));
routes(app); //moving the routes to routes folder
app.use('/public/',
express.static(path.join(__dirname, '../public')));
if('development' === app.get('env')){
app.use(errorHandler());
}
return app;
};
In a terminal, I enter node server.js
. I get an error:
error returned by trying to run server.js
在终端中,我输入node server.js
. 我收到一个错误:尝试运行 server.js 返回的错误
In the configure.js
file, I think I understand why I'm getting the error - the routes = require('./routes'),
statement seems to be requiring a module that doesn't exist yet, because the book hasn't described how to create that, and it doesn't seem to have been installed via the earlier npm command. But, I'm new at this (as I keep saying) so I might be wrong about that. Can anyone help me figure out what's going wrong here?
在configure.js
文件中,我想我明白为什么我会收到错误 - 该routes = require('./routes'),
语句似乎需要一个尚不存在的模块,因为这本书没有描述如何创建它,而且似乎也没有已通过较早的 npm 命令安装。但是,我是新手(正如我一直在说的那样),所以我可能是错的。谁能帮我弄清楚这里出了什么问题?
Also, if you happen to want to recommend a good book for learning Express, I'm all ears - maybe I should just ditch this book and start with one that teaches more of the fundamentals before jumping into creating a sample app.
此外,如果您碰巧想推荐一本学习 Express 的好书,我完全没听错——也许我应该放弃这本书,从一本教更多基础知识的书开始,然后再开始创建示例应用程序。
回答by Joseph
You're understanding is correct. When you require a file like so
你的理解是对的。当您需要这样的文件时
routes = require('./routes')
then nodejs is going to look for a routes.js file in your root path of your site.
然后 nodejs 将在您站点的根路径中查找 routes.js 文件。
Since you haven't created that file yet, it's going to bomb as soon as you hit that require.
由于您尚未创建该文件,因此一旦您达到该要求,它就会爆炸。
Additionally, the routes are usually set up like this
此外,路线通常是这样设置的
routes = require('./routes')
then some time later when you have your express app created you'll
然后一段时间后,当您创建了 Express 应用程序时,您将
app.use('/', routes); //sets up these routes on a base '/' route for your site
and then in routes.js you may do something like
然后在 routes.js 你可以做类似的事情
var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
res.render('hey this worked');
});
router.get('/another/route', function(req, res, next) {
res.json({ hello: 'world' });
});
module.exports = router;
回答by Chekwech Emma Isaiah
I have been using the same book and i encountered the same problem of Cannot find module ./routes
.
我一直在使用同一本书,但遇到了同样的问题 Not find module ./routes
。
At the time of writing that book, however, the npm express version was 4.16.4 and it included the routes module. A solution is to install routes like so:
然而,在写这本书的时候,npm express 版本是 4.16.4,它包含了 routes 模块。一个解决方案是像这样安装路由:
- Either
npm install [email protected]
or npm install [email protected] [email protected]
ornpm install [email protected]
- 无论是
npm install [email protected]
或 npm install [email protected] [email protected]
或者npm install [email protected]