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

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.

查看更多
我命由我不由天
3楼-- · 2019-01-30 02:48

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

查看更多
Evening l夕情丶
4楼-- · 2019-01-30 02:49
  • 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

查看更多
Ridiculous、
5楼-- · 2019-01-30 02:50

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
查看更多
一夜七次
6楼-- · 2019-01-30 02:50

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

查看更多
甜甜的少女心
7楼-- · 2019-01-30 02:56

Set NODE_PATH=NODE_HOME\node_modules.

I'm using windows 7 and it works fine.

查看更多
登录 后发表回答