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.
If you have run the npm install command before, then it's possible that the installation of the module is incomplete or corrupted. Delete the node_modules/ folder using the rm -rf node_modules command, then run npm install again. That may fix the issue. Finally, the same error can happen when you require() a local .
If you are getting the "Cannot find module" error when trying to run a local file, make sure that the path you passed to the node command points to a file that exists. For example, if you run node src/index. js , make sure that the path src/index. js points to an existing file.
To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' . Copied!
To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With