Getting error, Error: Cannot find module ‘express’

2020-02-17 02:15发布

问题:

I am new to both Node JS and express, and I have just installed npm in Windows 7.

I have installed express using the global flag:

npm install -g express

This sucessfully installs express in C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\express, and adds C:\Users\USER_NAME\AppData\Roaming\npm to my path.

However, using express in the command line is not successful. Any command starting with express tells me it cannot find the command to execute:

 'express' is not recognized as an internal or external command,
  operable program or batch file.

Looking in the installation folder, I can't find an executable file either (an .exe or a .cmd). Googling past questions shows that the node_modules folder (at least in past versions) should contain both an express folder (which I have) and a .bin folder, which contains express.cmd. I don't appear to have the .bin folder.

npm --version gives me 1.4.3. Express version looks to be 3.0.0

I've also tried installing it locally, and I've tried running npm cache clean and reinstalling. Neither method changes the situation: there's nothing executable in the node_modules folder.

Am I installing express incorrectly? If so, why does it not include an executable file?

回答1:

I ran into the same problem on Windows 8.1. The express.cmd is not created, but I found the text file:

C:\Users\you\AppData\Roaming\npm\node_modules\express\Readme.md

It suggests to run this:

npm install -g express-generator@3

Which will download more stuff.

After that you can use express on the windows command prompt. It will be in your path ( C:\Users\you\AppData\Roaming\npm)

Edit:

express-generator@3 is now updated to express-generator@4, so use this instead,

npm install -g express-generator@4

The answer isn't complete, because the modules are installed in C:\Users\you\AppData\Roaming\npm directory as stated above, and you cannot always access any module without, 1) linking it to your current project or 2) explicityly defining the NODE_PATH system variable pointing your node to the right place in the system.

First method,

After installing the module (express in our case), you can link it to your current project by going to your current project directory using cmd and executing below command,

npm link express

You will get a message like this if it is successfully linked,

D:\Project\node_modules\express -> C:\Users\Sufiyan\AppData\Roaming\npm\node_modules\express

(you cannot link directories without running cmd with Administrator privileges)

The second option is to create or update NODE_PATH system variable pointing your node to the right place in the system. Read this for details.

Also read this official Node.js documentation regarding the issue,

http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/



回答2:

This is all you need to do:

C:\node> npm install -g express-generator

You can find this and much more at this detailed start up tutorial.



回答3:

To fix this you need to use:

npm install -g express-generator@3

It has been updated from the previous command:

npm install -g express



回答4:

Just be aware that the version as return by the express --version command will be the version of express-generator, not express.



回答5:

I had this problem, Seems though we use -g it's ignored.

I just copied contents of MyProject/source/vendor to C:\Users\username\AppData\Roaming\npm

Copy only needed folders inside node_modules (like: express, express-generator, bower, yo, gulp and packages that should be in PATH).


This problem really bothers everyone (or windows users?), npm programmers should solve it!



回答6:

Run cmd as administrator. It solved the 'express' not found problem in my case



回答7:

npm link express to link express from any directory to your project directory were node is, by default when you run npm install express it would be installed in C:\Users\****\AppData\Roaming\npm\node_modules\express, so you don't have to install express and other frameworks required to install each time in your project directory, instead you can point to your node i.e where you have your application.