Node.js Error: cannot find express module

I wrote my first node.js application, but it cannot find the express library:

C:\ChatServer\Server>node server.js module.js:340 throw err; ^ Error: Cannot find module 'express' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at Object.<anonymous> (C:\ChatServer\Server\server.js:6:9) at Object.<anonymous> (C:\ChatServer\Server\server.js:25:4) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) 

But the expression is intalled (with the -g switch):

 C:\ChatServer\Server>npm install -g express npm http GET https://registry.npmjs.org/express npm http 304 https://registry.npmjs.org/express npm http GET https://registry.npmjs.org/connect/2.7.11 npm http GET https://registry.npmjs.org/commander/0.6.1 npm http GET https://registry.npmjs.org/range-parser/0.0.4 npm http GET https://registry.npmjs.org/mkdirp/0.3.4 npm http GET https://registry.npmjs.org/cookie/0.1.0 npm http GET https://registry.npmjs.org/buffer-crc32/0.2.1 npm http GET https://registry.npmjs.org/fresh/0.1.0 npm http GET https://registry.npmjs.org/methods/0.0.1 npm http GET https://registry.npmjs.org/send/0.1.0 npm http GET https://registry.npmjs.org/cookie-signature/1.0.1 npm http GET https://registry.npmjs.org/debug npm http 304 https://registry.npmjs.org/mkdirp/0.3.4 npm http 304 https://registry.npmjs.org/range-parser/0.0.4 npm http 304 https://registry.npmjs.org/cookie/0.1.0 npm http 304 https://registry.npmjs.org/connect/2.7.11 npm http 304 https://registry.npmjs.org/commander/0.6.1 npm WARN package.json range-parser@0.0.4 No repository field. npm http 304 https://registry.npmjs.org/buffer-crc32/0.2.1 npm http 304 https://registry.npmjs.org/fresh/0.1.0 npm http 304 https://registry.npmjs.org/methods/0.0.1 npm http 304 https://registry.npmjs.org/send/0.1.0 npm http 304 https://registry.npmjs.org/cookie-signature/1.0.1 npm WARN package.json methods@0.0.1 No repository field. npm WARN package.json methods@0.0.1 No readme data. npm WARN package.json fresh@0.1.0 No repository field. npm WARN package.json send@0.1.0 No repository field. npm WARN package.json cookie-signature@1.0.1 No repository field. npm http 304 https://registry.npmjs.org/debug npm http GET https://registry.npmjs.org/mime/1.2.6 npm http GET https://registry.npmjs.org/qs/0.6.5 npm http GET https://registry.npmjs.org/send/0.1.1 npm http GET https://registry.npmjs.org/formidable/1.0.14 npm http GET https://registry.npmjs.org/cookie/0.0.5 npm http GET https://registry.npmjs.org/bytes/0.2.0 npm http GET https://registry.npmjs.org/pause/0.0.1 npm http 304 https://registry.npmjs.org/mime/1.2.6 npm http 304 https://registry.npmjs.org/bytes/0.2.0 npm WARN package.json bytes@0.2.0 No repository field. npm http 304 https://registry.npmjs.org/pause/0.0.1 npm WARN package.json pause@0.0.1 No repository field. npm http 304 https://registry.npmjs.org/formidable/1.0.14 npm http 304 https://registry.npmjs.org/qs/0.6.5 npm http 304 https://registry.npmjs.org/send/0.1.1 npm http 304 https://registry.npmjs.org/cookie/0.0.5 npm http GET https://registry.npmjs.org/mime npm http 304 https://registry.npmjs.org/mime C:\Users\Dmitry\AppData\Roaming\npm\express -> C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express\bin\express npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported. npm WARN package.json Please pick one as the 'repository' field npm WARN package.json assert-plus@0.1.2 No repository field. npm WARN package.json ctype@0.5.2 No repository field. express@3.2.6 C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express β”œβ”€β”€ methods@0.0.1 β”œβ”€β”€ fresh@0.1.0 β”œβ”€β”€ range-parser@0.0.4 β”œβ”€β”€ cookie-signature@1.0.1 β”œβ”€β”€ buffer-crc32@0.2.1 β”œβ”€β”€ cookie@0.1.0 β”œβ”€β”€ debug@0.7.2 β”œβ”€β”€ commander@0.6.1 β”œβ”€β”€ mkdirp@0.3.4 β”œβ”€β”€ send@0.1.0 (mime@1.2.6) └── connect@2.7.11 (pause@0.0.1, qs@0.6.5, bytes@0.2.0, cookie@0.0.5, formidable@1.0.14, send@0.1.1) 

Some information:

 C:\ChatServer\Server>npm --version 1.2.24 C:\ChatServer\Server>node --version v0.10.9 C:\ChatServer\Server>npm --version 1.2.24 C:\ChatServer\Server>npm ls -g installed express npm WARN package.json cookie-signature@1.0.1 No repository field. npm WARN package.json fresh@0.1.0 No repository field. npm WARN package.json methods@0.0.1 No repository field. npm WARN package.json range-parser@0.0.4 No repository field. npm WARN package.json send@0.1.0 No repository field. npm WARN package.json policyfile@0.0.4 'repositories' (plural) Not supported. npm WARN package.json Please pick one as the 'repository' field npm WARN package.json bytes@0.2.0 No repository field. npm WARN package.json pause@0.0.1 No repository field. npm WARN package.json assert-plus@0.1.2 No repository field. npm WARN package.json ctype@0.5.2 No repository field. C:\Users\Dmitry\AppData\Roaming\npm └── express@3.2.6 C:\ChatServer\Server>npm ls installed express C:\ChatServer\Server └── (empty) 

How can I solve this problem? ( MUST I install it without -g ?)

+117
npm express
Jun 18 '13 at 6:52
source share
14 answers

You need to install Express locally in the context of your application ( node_modules folder):

 $ npm install express 

The reason for this is that applications always look in their local context for any dependencies. A global installation is only intended to create system-wide binaries, such as unit test runners or bootstrappers or something like that.

With Express, when you install it worldwide, you get an express binary that can download the application for you. For more information, enter

 $ express --help 

So, to answer your last question: YES, you need to install it without -g .

+239
Jun 18 '13 at 6:56
source share

For me, this worked when locally installed locally with the -save option as follows:

 $ npm install express --save 
+17
Nov 12 '14 at
source share

Check if the express module is installed, use the following command:

  npm install express 

and if your node_modules directory is in another place, set the variable NODE_PATH envirnment:

  set NODE_PATH=your\directory\to\node_modules;%NODE_PATH% 
+10
Nov 22 '13 at 10:25
source share

Golo explained the solution well, but I could add an explanation:
sometimes node modules are installed in

 /usr/local/lib/node_modules 

and when node node blabla.js starts, it searches in

 /lib 

So, the solution is to create a symbolic link:

 sudo ln -s /usr/local/lib/node_modules/ /lib/node_modules 
+5
Nov 18 '13 at 16:16
source share

In your case, your express module is installed in C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express , but you need to connect this module to your project directory. Therefore, you must copy the file to the express folder with C:\Users\Dmitry\AppData\Roaming\npm\node_modules\ in the project directory as follows: C:\ChatServer\Server\node_modules . If the project folder does not have a folder named "node_modules", first create it and paste these files into this folder. This method worked on my windows computer. Reboot the node server and run the node C:\ChatServer\Server>node server.js . It should work now !!!!

+4
May 09 '14 at 6:17
source share

Ubuntu based OS you can try

 sudo apt-get install node-express 

his work for me at the Mint

+2
Sep 02 '15 at 16:57
source share

Given that you installed node on your system, install Express locally for your project, using the following for Windows:

 npm install express 

or

 npm install express --save 

You can provide global access using:

 npm install -g express --save 
+2
Sep 23 '16 at 12:59
source share

I had this error in vscode, although the modules where they were installed. I use typescript and express. In the server.ts files server.ts all imports are server.ts underlined in red. Turns out I had a defective tsconfig.json file.

 { "compileOnSave": false, "compilerOptions": { "module": "commonjs", // Previously this value was `es6` "target": "es6", "allowSyntheticDefaultImports": true, "baseUrl": "public", "sourceMap": true, "outDir": "dist", "jsx": "react", "strict": true, "preserveConstEnums": true, "removeComments": true, "noImplicitAny": true, "allowJs": true }, "exclude": [ "node_modules", "build" ] } 
+2
Aug 30 '17 at 22:59 on
source share

create one folder in your hard drive, for example sample1, and go to the command line: cd and specify the path to the sample1 folder, and then install all the modules ...

npm install express

npm install jade

npm install socket.io

and then no matter what you create to save the application in the sample1 folder

try ...

+1
Feb 20 '14 at 11:38
source share

go to the application directory and install the express module using the following command: npm install express --save then list all the installation modules using the following command: npm ls you will see all locally installed modules.

+1
Jan 07 '17 at 19:56 on
source share
 npm install; 

This worked for me on Linux.

0
Jul 31 '16 at 2:55
source share

I ran into the same problem. I was installed globally in / usr / local / bin /. When I do "npm install", the express was not created in the node_modules local directory.

  • Check if you have a .npmrc file name in $ HOME
  • If it has "global = true", change to "global = false"
  • Now do "npm install" in the application directory. Most likely, you should get all the package dependent modules installed in node_modules (local) in the application directory.
0
Nov 03 '16 at 23:55
source share
  • sudo brew remove node
  • brew update
  • brew upgrade
  • brew cleaning
  • brew install node
  • sudo chown -R $ (whoami) / usr / local
  • brew link --overwrite node
  • sudo brew postinstall node

It worked for me on MacOS X Sierra

0
Nov 26 '16 at 19:29
source share

If you already have Nodejs modules installed for another specific project, you can go to the root directory of your project and find a directory called 'node_modules'. copy this folder to the root directory of the new project and run the application again. Now it works.

Note: I'm not sure if we can load the express module from the directory directly into my app.js file instead of copying the 'node_modules' directory to the new directory itself.

0
Jun 13 '19 at 8:41
source share



All Articles