143

I'm getting the following error in the console: Error : Cannot find module.

Here is the full error. What should I do?

internal/modules/cjs/loader.js:582
    throw err;
    ^

Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:741:12)
    at startup (internal/bootstrap/node.js:285:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:739:3)
Henke
  • 4,445
  • 3
  • 31
  • 44
istiyak ahamed Milon
  • 1,441
  • 2
  • 10
  • 5

45 Answers45

199

For All => Windows, Linux, Mac

  1. Delete the node_modules directory
  2. Delete the package-lock.json file
  3. Run npm install
  4. Run npm start

For Linux

rm -rf node_modules package-lock.json && npm install && npm start
Parth kharecha
  • 6,135
  • 4
  • 25
  • 41
  • Check this https://stackoverflow.com/questions/31976722/start-script-missing-error-when-running-npm-start – Parth kharecha Jul 02 '20 at 17:22
  • 23
    I acknowledge that this answer has received a lot of upvotes. However, I would rather strongly suggest being cautious the advice given here. The answers by caseyjustus, ttfreeman, Bishwajit Vikram, Muhammad Tahir, Andrew Koper all have in common that the error in the question shows up when `node` is called on _a file that is actually not there_.(!) -- Reinstalling `Node.js` should only be done as a last resort - when everything else has failed. You might save yourself some trouble by first reading [this answer](https://stackoverflow.com/questions/53545800#62740853). – Henke Jul 05 '20 at 15:53
  • @HenkeMaybe answer changed, but it reinstalls dependencies - not node,js – reducing activity Nov 16 '22 at 17:48
  • The essense of this answer is _update everything_. In my case trying to run `jsdoc` it was not node_modules, but node itself that was the issue. After `choco upgrade nodejs` lifted me from node version 13.0.1 to 19.6.0 - JSDoc started working. – Peer Sommerlund Feb 17 '23 at 16:44
  • Yeah @Henke was right - I was in the wrong directory. So the file was not there. Once I switched to the right directory, it worked. Duh. On the other hand - it would be good if NodeJs just said "file not found!", instead of this serious sounding error! – a20 Apr 14 '23 at 15:31
  • I cannot delete the `node_modules` and `package-lock.json` files since they are in a shared directory (google drive) which would affect other devices using them... – D.L Jun 13 '23 at 10:56
  • That's not work for me, sir. I've been remove my node modules and package.json and then re-install npm again. But everything was the same – Muhammad Sholehhudin Jul 11 '23 at 13:25
  • After completing the above steps, duplicate the .env.example(if present else create new) file and rename it to .env – inhaq Aug 28 '23 at 11:55
37

I had the same issue when I first tried on node js.
I noticed this issue was happening to me because I had some .js files with same names in different directories, which were in the same main directory.
I created another directory outside the main project folder, and created a .js file.
After that, it ran fine.
ex- app.js

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
  • 168
    I didn't understand what you mean – Tito Jun 18 '20 at 08:50
  • 1
    @Tito In my particular case, I was running a package.json script which was `node 'Server/index.js'` and should have been `node index.js` instead. I think that's the translation of this answer as well. – Andrew Jan 22 '21 at 05:53
24

try the following command

remove node_modules and package-lock.json

rm -rf node_modules package-lock.json

then run the following command to install dependencies

npm install

finally, run your package by the following command.

npm start
Mohit Rathod
  • 1,057
  • 1
  • 19
  • 33
9

What helped me was to place the .js file that I was working with in a new folder, drag and drop that folder into VS Code (to open the directory directly in VS Code), open the terminal in VS Code, and then simply type node <filename>.js (or in my case node index.js).

I had already installed node on my system, but for whatever reason, I was still getting the error that you've mentioned, even when I typed the direct path to the file i.e. node /desktop/index.js.

So, creating a new folder on my desktop, placing the .js file inside that folder, opening that folder within VS Code, and then typing node index.js in the terminal solved my issue.

HappyHands31
  • 4,001
  • 17
  • 59
  • 109
7

I was having the same error because I had a space at the end of my filename(not the reference but the actual filename). Once I changed 'app.js ' to 'app.js' it worked fine.

caseyjustus
  • 171
  • 2
  • 5
6

Replace your file name in package.json ({"npm": <your server code file name>.js}) with that file where your server code is running (it should be app.js, main.js, start.js, server.js, or whatever you picked up).

Al.G.
  • 4,327
  • 6
  • 31
  • 56
Kuldeep
  • 75
  • 3
  • 8
6

1. Read the error message!

Read the error message. The first line of the second part says :

Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'

So the first thing to check is: does the file exist?

On Windows, open a command prompt (WinKey+r, type cmd, press Enter). Run :

dir C:\Users\User\Desktop\NodeJsProject\app.js

If the response is – The system cannot find the path specified.orFile Not Found – then you know that the file doesn't exist.


Likewise, on Linux/Unix (in my case MSYS2 on Windows), run

$ ls "C:\Users\User\Desktop\NodeJsProject\app.js"
ls: cannot access 'C:\Users\User\Desktop\NodeJsProject\app.js': No such file or directory

Thus, if on Linux you get No such file or directory, then you know that the file doesn't exist.

2. Try running npm install

If Section 1 above didn't solve your problem, try running npm install, then npm start.

3. Last resort: try this answer

If none of the above solved your problem, my last tip is to :

  1. delete the node_modules directory,
  2. delete the package-lock.json file,
  3. run npm install,
  4. run npm start.

4. If the error happens in VS Code

Note:
The error might occur for no apparent reason when debugging in Visual Studio Code.
If you get the error inside VS Code, see if this answer helps.

5. How to reproduce your error

Presumably, you've already installed Node.js on your computer.
If not, download and install.

The error message is easy to reproduce. After installing Node.js, open a command line, and run :

$ node thisFileDoesNotExist.js
node:internal/modules/cjs/loader:1078
  throw err;
  ^

Error: Cannot find module 'C:\thisFileDoesNotExist.js'
[90m    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)[39m
[90m    at Module._load (node:internal/modules/cjs/loader:920:27)[39m
[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)[39m
[90m    at node:internal/main/run_main_module:23:47[39m {
  code: [32m'MODULE_NOT_FOUND'[39m,
  requireStack: []
}

Node.js v18.14.2

6. How to run node <someFile.js> without an error

To run Node.js in the terminal without an error, in the Windows command line, run :

echo console.log('\nHello world!')> hello.js
node hello.js

In a Linux terminal, try:

echo "console.log('\nHello world\!\n')"> hello.js
node hello.js

In both cases, expect the response to be :

Hello world!

If you delete hello.js, and then run node hello.js, you should once again get the error you asked about.

References

Henke
  • 4,445
  • 3
  • 31
  • 44
6

For those who are using TypeScript, it's caused by incremental option in the compilerOptions of your settings.

This causes to build tsconfig.tsbuildinfo file which stores all the data for cache. If you remove that file and recompile the project it should work straight away.

eslotwinski
  • 121
  • 1
  • 6
5

The particular .js file was in the sub folder (/src) of the application and Terminal was in general App folder.(which contains all package files,modules,public folder,src folder) it was throwing that error.Going to (/src) of application resolved my issue.

Madrx8
  • 3
  • 1
Muhammad Tahir
  • 2,351
  • 29
  • 25
5

Ran into a similar issue with nodemon running through docker,

it'd be worth checking that your "main" file in your package.json is configured to point at the correct entry point

in package.json

"main": "server.js",
"scripts": {
    "start":"nodemon src/server.js",
    "docker:build": "docker build -f ./docker/Dockerfile . "
  },
Kingswell
  • 116
  • 1
  • 2
4

Make sure you give the right address path for app.js when running node <path>/app.js. It can't find it

Error: Cannot find module 'C:\Users\User\Desktop\NodeJsProject\app.js'
ttfreeman
  • 5,076
  • 4
  • 26
  • 33
4

It's possible you are not running the terminal command from the right directory.

If you have created a new folder for example, consider navigating into the folder, then run the command from there.

somto
  • 51
  • 3
3

When I was using the below command, I too was getting the same error:

node .function-hello.js

I changed my command to below command, it worked fine:

node .\function-hello.js
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
3

The path to the js file you're trying to execute is wrong; you have to type the path and the file name you want to execute relative to root where node is, but what you typed isn't where it is.

I typed node redux-basics.js, got this slightly-misleading error message, Stack Overflow'ed, looked at my file system, and I should have typed node src/redux-basics.js.

Andrew Koper
  • 6,481
  • 6
  • 42
  • 50
2

Something weird happened to me last night. I ran command node run watch instead of npm run watch. I tried doing everything on this thread but nothing worked out for me. I was frustrated but eventually noticed that I ran the command wrong. I was laughing out loud. Sometimes this things happened. Enjoying learning Nodejs though.

Mr.spShuvo
  • 428
  • 4
  • 12
2

For me "npm install" again from command prompt worked. Command Prompt must be "Run as Administrator"

saransh mehra
  • 157
  • 2
  • 9
2

Create the .js file inside the main directory not inside the sub folders such as public or src.

Sohail
  • 43
  • 6
2

Simply type "node NodeJsProject/app.js"

Your program will run :)

AshPython
  • 51
  • 2
2

Go to your package.json file and check this line:

"main": "main.js

File provided in " brackets must be exactly the file you are trying to run with

node main.js

This solved my issue

2

I've got this same issue when working in typescript with nestjs. For whatever reason after trying to run debugging in VS Code for the first time (with launch.json - which didn't work btw.)

What worked for me was: remove dist folder (the one with generated .js files from .ts files, path to this folder is usually specified in tsconfig.json).

Simple solution, but worked magic :)

Dharman
  • 30,962
  • 25
  • 85
  • 135
yrk
  • 350
  • 3
  • 8
2

i accidentally moved a file outside of /src, that changed the structure of the resultant /build directory, so now in package.json ... "dev:serve": "nodemon --inspect=4899 build/index.js", didnt exist... it was now in build/src/index.js

1

same happen to me i just resolved by deleting "dist" file and re run the app.

bangash
  • 398
  • 1
  • 4
  • 12
1

For me, the Node package I was trying to use would only work on an older version of Node.

I was able to fix it by using Homebrew to install an older version of Node:

brew unlink node
brew install node@12
echo 'export PATH="/usr/local/opt/node@12/bin:$PATH"' >> ~/.zshrc

In the above commands, you will need to edit the Node version and then export PATH command.

Jonathan Hult
  • 1,351
  • 2
  • 13
  • 19
1

The below commands resolved the issue for me.

npm install node-gyp -g
npm install bcrypt -g

npm install bcrypt -save
Mab Kiani
  • 576
  • 5
  • 16
1

I uninstalled puppeteer, mocha and chai using

npm uninstall puppeteer mocha chai

from the command line and then reinstalled using

npm install puppeteer mocha chai

and the error message simply never showed up

beegee Assem
  • 79
  • 1
  • 3
  • 16
1

I got the same error:

 nodemon -w server.js server.js

[nodemon] 2.0.2
[nodemon] reading config .\nodemon.json
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 19248 to restart
[nodemon] ignoring: .\node_modules/**/* .\.next/**/*
[nodemon] watching dir(s): server.js
[nodemon] watching extensions: js,json
[nodemon] starting `node server.js index.js`
[nodemon] forking
[nodemon] child pid: 18840
[nodemon] watching 30 files
internal/modules/cjs/loader.js:797
    throw err;
    ^

Error: Cannot find module 'D:\Programming\01a.nextjs\project\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...

I followed all the advises from here, but none of them worked for me. What I found is that I moved the server.js in his own folder server/server.js, but in package.json I forgot to make the change from this:

 "dev": "nodemon -w server.js server.js",
 "build": "next build",
 "start": "NODE_ENV=production node server.js"

to this:

"dev": "nodemon -w server/server.js server/server.js",
"build": "next build",
"start": "NODE_ENV=production node server/server.js"

After I made this change and restart the server with npm run dev everything worked fine.

dragon
  • 1,212
  • 13
  • 24
1

I changed name of my Project's folder and It's worked , i don't know why :)

  • 1
    I happened to me, but I believe is the name of the parent folder confuses npm because of special characters should it have. – StvnSpnz May 31 '20 at 19:59
1

you need start server use follow command

npm start

or

yarn start
zhulinpinyu
  • 599
  • 7
  • 12
1

it finally worked for me after I did sudo npm i cjs-loader (and make sure to install express, not just express-http-proxy)

William
  • 491
  • 5
  • 9
1

if you're working with node/express consider running this in your terminal

npm i cjs-loader
1

For me I just installed the package and it worked:

yarn add cjs-loader --save

or

npm i cjs-loader --save

But notice that sometimes the problem is not only in the absence of this lib, but the path of your application file app.js may not be inside this folder:

C:\Users\User\Desktop\NodeJsProject\
Ivan Ferrer
  • 558
  • 1
  • 5
  • 12
1

It ran with me after repairing the node.js installation from the add/remove programs.

This was globally, not for only one project!

user2895965
  • 51
  • 1
  • 8
1

For me my problem was with my Procfile. I had

web: node src/index.js

When I should have had:

web: node dist/index.js
JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71
1

I also suffered from this error but the solution was the simple I was running the node app on the different directory than the main where was the app.js was located so I'll suggest you to check the path.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 12 '23 at 13:12
0

Caseyjustus comment helped me. Apparently I had space in my require path.

const listingController = require("../controllers/ listingController");

I changed my code to

const listingController = require("../controllers/listingController");

and everything was fine.

Cuado
  • 501
  • 1
  • 5
  • 13
0

While tinkering with node 14 and trying to get both CJS and MJS to work I managed to add a package.json file in my server directory with

{ type: "module" }

I forgot I added this file and this was a cause of several wasted hours of looking at this error. Maybe this will help someone!

Tim Hysniu
  • 1,446
  • 13
  • 24
0

I placed my server.js (Whatever in your case) file in the wrong directory XD

0

I resolved it by using ts-node instaed of tsc && node

Hamid Shoja
  • 3,838
  • 4
  • 30
  • 45
0

First delete/remove node_modules file, then run this code:

npm cache clean -force

npm install -force

npm start

I hope it will solve your problem

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
0

My error shown was : Debugger attached. Waiting for the debugger to disconnect... node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module 'C:\Users\joel.sebastian\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js'........{ code: 'MODULE_NOT_FOUND', requireStack: [] }

and what went wrong was azure function tools installation and I install that by : npm install -g azure-functions-core-tools@3 --unsafe-perm true if again showing error of files exists, add force: npm install -g azure-functions-core-tools@3 --unsafe-perm true --force

JoelSebstn
  • 522
  • 4
  • 5
0

Usually for me this is because of azure function tools module

Debugger attached. Waiting for the debugger to disconnect... internal/modules/cjs/loader.js:905 throw err;

Error: Cannot find module 'C:\Users\myName\AppData\Roaming\npm\node_modules\azure-functions-core-tools\lib\main.js'

So for you find the missing module mentioned in the last line and either install or update it

JoelSebstn
  • 522
  • 4
  • 5
0

for me, moving my working directory to the location where node was located, solved the issue.

my node location- C:\Users\hp

my past working directory location- D:/Devslane/mywebsocket

my new working directory path- C:\Users\hp

Then open the directory from new location, delete present node modules, package-lock.json and 'npm install' them again.

Now run node <filename>.js and boom!

navinrangar
  • 904
  • 10
  • 20
0

After creation of custom lib from the official documentation

nest g library my-library

AND even running

npm run prebuild

I was always getting the same error because of the files ("libs" dir) outside of src that were being compiled.

enter image description here

Here was my dist folder:

enter image description here

I solved with adding "libs" for excluded files in tsconfig.build.ts

enter image description here

Elmatsidis Paul
  • 385
  • 1
  • 7
  • 19
0

This might be an another reason apart from above answers so I share this answer since it might help.

In my case my initial project path was F:\Tutorials\Node & Links\node-and-links-fe.

Node allows many ways to require a file (for example, with a relative path or a pre-configured path), before we can load the content of a file into the memory node need to find the absolute location of that file.

When we require a module, without specifying a path, Node will look for that module in all the paths specified by module.paths — in order.

The paths list is basically a list of node_modules directories under every directory from the current directory to the root directory.

If Node can’t find that module/file in any of these paths, it will throw a “cannot find module error.”

In my case, With the '&' special character in the project path, npm couldn't find the node_module path correctly since it effected to the generated absolute path it confuses npm when find the module.

So I change my project folder name removing '&' and as F:\Tutorials\Node and Links\node-and-links-fe. Then this issue was solved.

You can get better understanding on how npm works under the hood in this reference. Requiring modules in Node.js: Everything you need to know

DevThiman
  • 920
  • 1
  • 9
  • 24
0

Remix users should follow these steps:

Delete the node_modules directory
Delete the package-lock.json file
Run npm install
Run npm start
Duplicate the .env.example file and rename it to .env

inhaq
  • 336
  • 1
  • 6
  • 15