Here is the project structure:
/ app.js package.json /node_modules /app config.json /frontend assets and html tpls /modules couch.js raeume.js users.js
I require config.json, raeume.js and users.js from app.js and it all works fine.
var config = require('./app/config'); var raeume = require('./app/modules/raeume'); var users = require('./app/modules/users');
Then I require config.json and couch.js from user.js the same way and it won't find anything.
var couch = require('./app/modules/couch'); var config = require('./app/config');
I guess it should find it. After some research I saw a diverse landscape of problems inclusive how node is compiled. Thus included: I work on osx 10.8 with node v0.10.7.
To solve the "Cannot find module" error in Node. js, make sure to install the package from the error message if it's a third party package, e.g. npm i somePackage . If you get the error with a local module, make sure to point the node command to a file that exists.
If there is a package. json already existing in your project folder, then from command line you need to go to your project folder and type npm start. npm install --save grunt // And you need to do for all the node_modules, by replacing the **grunt**. Automatically the dependency will be added to your package.
You can think of the require module as the command and the module module as the organizer of all required modules. Requiring a module in Node isn't that complicated of a concept. const config = require('/path/to/file'); The main object exported by the require module is a function (as used in the above example).
A module not found error can occur for many different reasons: The module you're trying to import is not installed in your dependencies. The module you're trying to import is in a different directory. The module you're trying to import has a different casing.
The path is relative to the directory in which you are require
ing the files, so it should be something like:
var couch = require('./couch'); var config = require('../config');
A bit of clarification, if you write
var couch = require('./couch');
you are trying to require
the couch module which resides in the current directory, if you write
var couch = require('couch');
you are trying to require
the couch module installed via npm
.
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