Hugolpz
Hugolpz

Reputation: 18248

NodeJS - How to resolve “Cannot find module” error on local modules?

I'am aware the best npm install is to use npm install, but it is forbidden on the professional server I use. Also,I must install my node_modules via a dirty git clone <node_module_git_repository>.

Via git, my modules are copied well and runs fine via :

node ./node_modules/.bin/http-server

But fail via :

node http-server

I get back the error:

module.js:340
    throw err;
          ^
Error: Cannot find module '/data/yug/projects_active/make-modules/http-server'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:935:3

I assume that not doing npm install make that my node modules are not in path. How could I fix that to my git-cloned local modules runs via node <module_name> ?

Upvotes: 1

Views: 5077

Answers (1)

Peter Lyons
Peter Lyons

Reputation: 146044

The incantation you are asking about won't work. The node executable expects a path to a javascript file to execute. The path can be relative to the current working directory or absolute, but that's it. It does not search node_modules or NODE_PATH like require does from within a node.js program.

When you run ./node_modules/.bin/http-server your shell is executing that file, which starts with a shebang line something like #!/usr/bin/env node which handles running node and passing it the path to the http-server file for you.

If you are set on wanting node http-server to work, create a symlink in the make-modules directory: cd make-modules && ln -nsf ./node_modules/.bin/http-server.

Upvotes: 2

Related Questions