'Node error: "Cannot find module 'routes'"
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!
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.
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!
My folder structure is: Folder Structure The 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
So far, I have two files, the contents of which were copied straight from the book: 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.)
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
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?
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.
Solution 1:[1]
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.
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
app.use('/', routes); //sets up these routes on a base '/' route for your site
and then in routes.js you may do something like
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;
Solution 2:[2]
I have been using the same book and i encountered the same problem of Cannot 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:
- Either
npm install [email protected]or npm install [email protected] [email protected]ornpm install [email protected]
Solution 3:[3]
I have an auth.js file in my MERN application and in index.js I was using it asapp.use(require("./routes"));. It was working fine but after some code changes, it started giving error as:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module './routes'
Require stack:
After changing it to as:
app.use(require('./routes/auth'))
It started working again. So, if you came to this page, looking for this error, then you can try this.
Solution 4:[4]
If you have this problem you can check your routes file name and check carefully
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Joseph |
| Solution 2 | Preston Hager |
| Solution 3 | Suryakant |
| Solution 4 | Dharman |
