Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js require cannot find custom module

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.

like image 437
thgie Avatar asked May 20 '13 15:05

thgie


People also ask

How do I resolve Cannot find module error using NodeJS?

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.

How fix npm module not found?

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.

How do I use require in NodeJS?

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).

Can not find module imported from?

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.


1 Answers

The path is relative to the directory in which you are requireing 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.

like image 53
Alberto Zaccagni Avatar answered Oct 12 '22 01:10

Alberto Zaccagni