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

for mac users

cd /usr/local/lib/node
sudo ln -s ../node_modules/* ./$1
查看更多
倾城 Initia
3楼-- · 2019-01-30 02:37

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

查看更多
孤傲高冷的网名
4楼-- · 2019-01-30 02:38

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

If you need both, use the npm link command.

查看更多
何必那么认真
5楼-- · 2019-01-30 02:38

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.

查看更多
淡お忘
6楼-- · 2019-01-30 02:39

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/

  1. At your test folder or your folder:

sudo npm install -g express@2.5.8

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

查看更多
▲ chillily
7楼-- · 2019-01-30 02:41

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

查看更多
登录 后发表回答