67

I'm getting following Console Error. Error : Cannot find module

Here is the full error i'm getting in console. 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
  • 1,466
  • 2
  • 9
  • 22
  • 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 does not exist_.(!) -- Consider reading [https://stackoverflow.com/questions/53545800#62740853](https://stackoverflow.com/questions/53545800#62740853). – Henke Jul 06 '20 at 11:07
  • I was facing same problem.. I downgraded webpack-dev-server to 2.5.1 version. Now everything is working fine . – Nikita Bhutada Jan 30 '21 at 06:42

31 Answers31

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

OR

rm -rf node_modules package-lock.json && npm install && npm start
Parth kharecha
  • 3,353
  • 2
  • 18
  • 31
  • Check this https://stackoverflow.com/questions/31976722/start-script-missing-error-when-running-npm-start – Parth kharecha Jul 02 '20 at 17:22
  • 7
    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
  • This worked properly! – Rohit Parte Jan 06 '21 at 10:15
20

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,048
  • 4
  • 23
  • 41
  • 76
    I didn't understand what you mean – Tito Jun 18 '20 at 08:50
  • @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
14

try following command

remove node_modules and package-lock.json

rm -rf node_modules package-lock.json

then run following command to install dependencies

npm install

finally, run your package by following command.

npm start
Mac Rathod
  • 621
  • 1
  • 10
  • 26
6

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
  • 131
  • 2
  • 4
5

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
  • 3,503
  • 6
  • 40
  • 78
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.

Muhammad Tahir
  • 1,984
  • 24
  • 25
4

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.
  • 3,929
  • 6
  • 32
  • 52
Kuldeep
  • 51
  • 3
  • 7
3

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
  • 3,082
  • 2
  • 20
  • 28
3

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
  • 41
  • 2
2

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
  • 89
  • 1
  • 1
  • 16
2

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,740
  • 21
  • 43
  • 62
Bishwajit
  • 55
  • 7
2

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

saransh mehra
  • 147
  • 2
  • 8
2

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
  • 4,049
  • 5
  • 32
  • 41
2

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.

1

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

bangash
  • 323
  • 4
  • 11
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,116
  • 2
  • 11
  • 16
1

The below commands resolved the issue for me.

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

npm install bcrypt -save
Mab Kiani
  • 559
  • 5
  • 15
1

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
  • 128
  • 10
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,134
  • 11
  • 21
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
  • 499
  • 6
  • 12
1

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

Sohail
  • 33
  • 6
1

This error message is easy to reproduce.

  • Open a terminal window.
    (On Windows: WinKey, cmd, Enter. On Linux: Ctrl + Alt + t.)
  • Type npm and hit Enter to see if Node.js is installed.
  • If you get command not found, download at https://nodejs.org/en/download/ and install.
    (On Linux/Ubuntu: sudo apt install nodejs if you prefer.)
  • Type (or paste) node thisFileDoesNotExist.js (and hit Enter).

On Windows expect to see something similar to:

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

Error: Cannot find module [... + a few more lines]

On Linux (Ubuntu 18.04):

module.js:549
  throw err;
  ^

Error: Cannot find module [...]

I have not tried macOS, but would expect something similar there as well.

Note: This might happen for no apparent reason when debugging in Visual Studio Code.
If you get the error inside VScode, see if the answer by HappyHands31 is of any help.


Finally, to run Node.js in the terminal without an error, in the Windows terminal (command line) try:

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

In the Linux terminal try:

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

Of course, expect to see the terminal responding:

Hello world!
Henke
  • 1,466
  • 2
  • 9
  • 22
1

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
  • 78
  • 9
1

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
  • 56
  • 2
1

Simply type "node NodeJsProject/app.js"

Your program will run :)

AshPython
  • 41
  • 2
1

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
  • 21,838
  • 18
  • 57
  • 107
yrk
  • 41
  • 5
0

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
  • 431
  • 3
  • 8
0

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

npm i cjs-loader
0

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

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

NEOdinok
  • 39
  • 7