I wrote my first node.js app, but it can't find 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 express is intalled (with -g
key):
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 [email protected] 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 [email protected] No repository field.
npm WARN package.json [email protected] No readme data.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 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 [email protected] No repository field.
npm http 304 https://registry.npmjs.org/pause/0.0.1
npm WARN package.json [email protected] 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 [email protected] 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
[email protected] C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected] ([email protected])
+-- [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected])
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 [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] No repository field.
C:\Users\Dmitry\AppData\Roaming\npm
+-- [email protected]
C:\ChatServer\Server>npm ls installed express
C:\ChatServer\Server
+-- (empty)
How can I solve this problem? (MUST I install it without -g
?)
I hit the same problem. I had express installed globally at /usr/local/bin/. When I do 'npm install', express was not created in node_modules of local directory.
Check if you are not install express
module, use this command:
npm install express
and if your node_modules
directory is in another place, set NODE_PATH
envirnment variable:
set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%
I've came across a similar problem and in the end it was a matter of some old dependencies that were messing up my Heroku server.
While at my project's folder I've run:
npm uninstall
npm install
I hope it helps
After you do express in your terminal, then do
npm install
To install all the dependencies.
Then you can do node app to run the server.
Have you tried
npm install
If you're specifically looking for just express
npm install --save express
go to your application directory and install the express module using the below command npm install express --save then list the all install module using the below command npm ls you will see all the locally install modules.
npm ERR! Error: ENOENT, open '/Users/feelexit/package.json'
This happens due to missing permissions or unlinked files while npm was working.
Meaning, that executing npm
as this user doesn't have enough rights to read/write from a file, in this case package.json.
try adding sudo
before the entire command - it should resolve.
$ sudo npm install -g express
$ Password:*******
Password would be your admin password of your mac.
-g
flag will install this module (express) in the global context of node - meaning node will/should recognize express
module from within any js file without having to provide a full path to the module in use.
Hope this helps!!
if youre main file is located at /Users/feelexit/WebstormProjects/learnnode/node_modules/index.js
then express needs to be located
at /Users/feelexit/WebstormProjects/learnnode/node_modules/node_modules
as node always looks for modules in ./node_modules
(and its internal folder)
when the path dont start with ./
or /
(more info here)
i think you miss placed youre main file in the module folder
Golo have explain well the solution, but I might add a clarification:
sometimes node modules are installed in
/usr/local/lib/node_modules
and when you launch node blabla.js modules are searched in
/lib
So a solution is to create a symbolic link:
sudo ln -s /usr/local/lib/node_modules/ /lib/node_modules
In rare cases, npm cache may get corrupt. For me, what worked was:
npm cache clean --force
Generally, the package manager will detect corruption and refetch on its own so this is not usually necessary. However, in my case Windows 10 crashed a few times and I suspect this may have been during a fetch operation. Hope it helps someone!
More information: https://docs.npmjs.com/cli/cache
I'm guessing that this is coursework from Colt Steele's Web Development course... I was looking for the same answer as to why I ended up with that error too.. Colt doesn't say so but you take the node_module folder and move into the new folder you're working in... that's what worked for me.
npm install from within your app directory will fix the issue as it will install everything required
1.first check if express is install at correct location. 2. npm install express (run this command). 3. express will save under your "node_modules" folder
You have your express module located in a different directory than your project. That is probably the problem since you are trying to require()
it locally. Try moving your express module from /Users/feelexit/nvm/node_modules/express to /Users/feelexit/WebstormProjects/learnnode/node_modules/express. This info can give you more detail about node_module file structures.
This worked for me on MacOS X Sierra
On Ubuntu-based OS you can try
sudo apt-get install node-express
its working for me on Mint
Check if you have installed express
module. If not, use this command:
npm install express
and if your node_modules
directory is in another place, set NODE_PATH
envirnment variable:
set NODE_PATH=your\directory\to\node_modules;%NODE_PATH%
Digging up an old thread here BUT I had this same error and I resolved by navigating to the directory my NodeApp resides in and running npm install -d
It says
Cannot find module 'express'
Do you have express installed? If not then run this.
npm install express
and run your program again.
for this scenario run npm install express command using your cmd prompt for the respective folder where you want to run the program. Example I want to run the express module program server.js in F:\nodeSample. So run "npm install express" in that particular folder then run server.js
D:\learn\Node.js\node app.js
module.js:549
throw err;
^
Error: Cannot find module 'body-parser'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
Sometimes version not match with package.json Fixed the problem by checking the package.json then use the following commands: npm install [email protected] it resolved for me.
create one folder in your harddisk e.g sample1 and go to command prompt type :cd and gives the path of sample1 folder and then install all modules...
npm install express
npm install jade
npm install socket.io
and then whatever you are creating application save in sample1 folder
try it...
Sometimes there are error while installing the node modules Try this:
In your case your express
module is installed at C:\Users\Dmitry\AppData\Roaming\npm\node_modules\express
, but you need to get this module in to your project directory. So you should copy the file the express
module folders from C:\Users\Dmitry\AppData\Roaming\npm\node_modules\
to your project directory as : C:\ChatServer\Server\node_modules
. If you do not have a folder named 'node_modules' in your project folder, then create it first and paste those files into this folder. This method worked for me on my windows pc. Restart your node server and once again run the command node C:\ChatServer\Server>node server.js
. It should work now !!!!
I had this error in vscode, although the modules where installed. I am using typescript and express. In the server.ts
files all the imports had red squiggly underlines. It turns out I had a faulty 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"
]
}
I had the same error following the example on this book: "Kubernetes Up & Running".
I see many answers suggesting to install express "by hand" but I'm not convinced is the best solution.
Because we are using package.json (I can see it in the logs) and the right way to build the app is running npm install
, I added the express dependency in the package.json file.
"dependencies": {
"express": "^4.17.1"
}
I get the current version with npm search express
.
For me it worked when installed express locally with --save option as follow:
$ npm install express --save
npm install --save express
This worked for me. Just run express.js installation again.
Unless you set Node_PATH, the only other option is to install express in the app directory, like npm install express --save
.
Express may already be installed but node
cannot find it for some reason
Run npm install express body-parser cookie-parser multer --save
command in the same directory with your source code nodejs file to resolve this issue.
P/s: check your directory after run command to understand more!
I had the same problem. My issue was that I have to change to the Node.js project directory on the command line before installing express.
cd /Users/feelexit/WebstormProjects/learnnode/node_modules/
Given you have 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 might give it global access by using:
npm install -g express --save
I'm not proud sharing this, but in my case I had:
require('express.handlebars')
//and the correct form is:
require('express-handlebars'); //Use dash instead.
installing express globally will not work on your local project so you need to install it locally for use .
npm install express
Hope this will work
Thank you
Source: Stackoverflow.com