Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Error: Cannot find module express

People also ask

How do I resolve Cannot find module error using node JS?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

Can not find the module Express?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.

Can not find module Dotenv?

To solve the error "Cannot find module 'dotenv'", make sure to install the dotenv package by opening your terminal in your project's root directory and running the following command: npm install dotenv and restart your IDE and development server.


You need to install Express locally into 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. The global installation is only for setting up system-wide available binaries, such as unit test runners or bootstrappers or things like that.

With Express, when you install it globally, you get an express binary that can bootstrap an application for you. For more information, type

$ express --help

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


For me it worked when installed express locally with --save option as follow:

$ npm install express --save

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%

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 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 !!!!


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