admin管理员组

文章数量:1135184

When I try to run the app.js file created by express, I get the following error:

$ node app.js

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Cannot find module 'express'
    at Function._resolveFilename (module.js:320:11)

When I type in express --version I get a return statement of 2.3.3. I used npm to install express. I had to manually make npm using these instructions:

git clone .git
cd npm
sudo make install

The error is Error: Cannot find module 'express'.

Do I need to do something after installing npm and express in order to make express see the modules created by npm?

  • My node is version: 0.4.6
  • My express is version: 2.3.3
  • My npm is version: 1.0.6

Express is installed globally. I used the -g flag to install it.


Edit: When I try "node -e require.paths" I get:

[ '/home/user/.node_modules',
  '/home/user/.node_libraries',
  '/usr/local/lib/node' ]

So, node isn't detecting the npm installation. How do I get node to detect the npm installation?

When I try to run the app.js file created by express, I get the following error:

$ node app.js

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
Error: Cannot find module 'express'
    at Function._resolveFilename (module.js:320:11)

When I type in express --version I get a return statement of 2.3.3. I used npm to install express. I had to manually make npm using these instructions:

git clone http://github.com/isaacs/npm.git
cd npm
sudo make install

The error is Error: Cannot find module 'express'.

Do I need to do something after installing npm and express in order to make express see the modules created by npm?

  • My node is version: 0.4.6
  • My express is version: 2.3.3
  • My npm is version: 1.0.6

Express is installed globally. I used the -g flag to install it.


Edit: When I try "node -e require.paths" I get:

[ '/home/user/.node_modules',
  '/home/user/.node_libraries',
  '/usr/local/lib/node' ]

So, node isn't detecting the npm installation. How do I get node to detect the npm installation?

Share Improve this question edited Feb 13, 2019 at 9:01 RobC 24.9k21 gold badges84 silver badges85 bronze badges asked May 7, 2011 at 7:08 KelpKelp 1,2685 gold badges18 silver badges32 bronze badges 6
  • 2 Your question doesn't specify if you have npm >=1.0, but if you don't you should upgrade. When you have done so, you should run npm install -g express to install it globally – this is important, since express has its own executable. – mikl Commented May 7, 2011 at 9:34
  • Hello I updated the main post with the versions of node, express, and npm that I am using. – Kelp Commented May 7, 2011 at 15:02
  • 1 When you installed express, did you install with npm install -g express or just npm install express? – nicolaskruchten Commented May 7, 2011 at 15:31
  • I added the -g argument. At first, I didn't, so I couldn't check the version of express installed using -g. Now, I am able to run commands using express in the shell. I typed: sudo npm install -g express – Kelp Commented May 7, 2011 at 23:17
  • 1 npm > 1.0 no longer installs global modules to /usr/local/lib/node. – Rob Raisch Commented May 17, 2011 at 0:04
 |  Show 1 more comment

14 Answers 14

Reset to default 72
  • Install express

    npm install -g express

  • Create a new app

    express your_app

  • cd into app directory

    cd your_app

  • use npm link to resolve modules

    npm link express

Use local installs for require(), and global installs for command-line apps.

If you need both, use the npm link command.

On Ubuntu 12.04 you have to add the export NODE_PATH=/usr/local/lib/node_modules to your /.bashrc to use globally installed modules.

It appears that while npm had been updated to install global modules into /usr/local/lib/node_modules, Node's own require.paths does not yet reflect this change.

There are two reasonable solutions:

  1. Add the following code to the top of your application:

    require.paths.push('/usr/local/lib/node_modules');
    
    • Pro: non-invasive, easy to add

    • Con: requires discipline, future versions of node will restrict access to require.paths

  2. As root, execute:

    ln -s /usr/local/lib/node_modules /usr/local/lib/node
    
    • Pro: reasonably non-invasive

    • Con: requires root, modifies linux fs, might not survive system updates

I had the same problem. This worked for me though:

Seems like npm (now?) installs node modules to /usr/local/lib/node_modules/ and not /usr/local/lib/node/

What I did was simply to copy everything from node_modules to node: sudo cp -r /usr/local/lib/node_modules/* usr/local/lib/node/ and now it seems to be working for me.

Hope this helps you :-)

What about NODE_PATH=/usr/local/lib/node_modules in .bashrc or .bash_profile? I think it's the real correct way.

Set NODE_PATH=NODE_HOME\node_modules.

I'm using windows 7 and it works fine.

It may happen, if you're using windows, that the environment variable NODE_PATH is not set, and thus when you execute node fileName.js it won't find the libraries.

Check for the variable on your console, and if not present, create it. Give it the NODE_HOME\node_modules value, where NODE_HOME is your node install dir. This path is where npm install puts every module upon downloading.

require.paths is removed, use the NODE_PATH environment variable instead.

It looks like the easiest way to do this is to run npm install from your app's folder. This tells npm to hook everything up.

It's the last instruction after express <appname>:

...
dont forget to install dependencies:
$ cd <appname> && npm install

Finally with Linux a good way to do is to use the command : sudo apt-get install node-express

But with express 4 we must use express-generator to make app skeleton, install it with 'npm install express-generator -g', and then run 'express myapp' command. see also install express

for mac users

cd /usr/local/lib/node
sudo ln -s ../node_modules/* ./$1

I installed gulp and when I ran this gulp command in the command line I got a gulp: command not found error. It appeared that it installed gulp in my local folder that is /home/YOURUSERNAME/.node/lib/node_modules and not in the global npm folder.

You can check npm root folder by running this command: npm root -g, which was returning my personal directory /home/YOURUSERNAME/.node/lib/node_modules and not the expected /usr/local/lib/node_modules.

You can fix this by running npm config set prefix /usr/local command.

For all problems with express with a mac computer:

The solution is:

  1. chown to your user the .npm folder :

    sudo chown -R Webmaste /Users/webmaste/.npm/
    
  2. At your test folder or your folder:

    sudo npm install -g [email protected]
    
  3. Invoke express from your actual location:

    /usr/local/share/npm/bin/express
    
  4. sudo cd . && npm install

  5. Finally:

    node app
    

the final message in the console should look like this:

Express server listening on port 3000 in development mode

本文标签: javascriptExpress module not found when installed with NPMStack Overflow