Express module not found when installed with NPM

2019-01-30 02:35发布

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?

14条回答
放荡不羁爱自由
2楼-- · 2019-01-30 02:59

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.

查看更多
虎瘦雄心在
3楼-- · 2019-01-30 03:03

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

查看更多
登录 后发表回答