Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error, Error: Cannot find module ‘express’ after npm install

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?

like image 699
kotoole Avatar asked Apr 11 '14 07:04

kotoole


People also ask

Can not find module npm?

Set the Windows environment variable for NODE_PATH. This path is where your packages are installed. It's probably something likeNODE_PATH = C:\Users\user\node_modules or C:\Users\user\AppData\Roaming\npm\node_modules. Start a new cmd console and npm should work fine.

How do I fix a node module error?

If you have run the npm install command before, then it's possible that the installation of the module is incomplete or corrupted. Delete the node_modules/ folder using the rm -rf node_modules command, then run npm install again. That may fix the issue. Finally, the same error can happen when you require() a local .

What is npm install express?

By default with version npm 5.0+ npm install adds the module to the dependencies list in the package. json file; with earlier versions of npm, you must specify the --save option explicitly. Then, afterwards, running npm install in the app directory will automatically install modules in the dependencies list.


1 Answers

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/

like image 176
user3523091 Avatar answered Nov 10 '22 01:11

user3523091