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

Javascriptnode.js

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Solution 3 - Javascript

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

Solution 4 - Javascript

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.

Solution 5 - Javascript

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.

Solution 6 - Javascript

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).

Solution 7 - Javascript

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.

Solution 8 - Javascript

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.

Solution 9 - Javascript

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'

Solution 10 - Javascript

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.

Solution 11 - Javascript

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

Solution 12 - Javascript

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 . "
  },

Solution 13 - Javascript

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

Solution 14 - Javascript

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.

Solution 15 - Javascript

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

Solution 16 - Javascript

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!

Solution 17 - Javascript

Simply type "node NodeJsProject/app.js"

Your program will run :)

Solution 18 - Javascript

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 :)

Solution 19 - Javascript

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

Solution 20 - Javascript

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

Solution 21 - Javascript

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.

Solution 22 - Javascript

The below commands resolved the issue for me.

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

npm install bcrypt -save

Solution 23 - Javascript

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

Solution 24 - Javascript

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.

Solution 25 - Javascript

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.

Solution 26 - Javascript

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

Solution 27 - Javascript

you need start server use follow command

npm start

or

yarn start

Solution 28 - Javascript

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.

Solution 29 - Javascript

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

Solution 30 - Javascript

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

Solution 31 - Javascript

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

npm i cjs-loader

Solution 32 - Javascript

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\

Solution 33 - Javascript

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

This was globally, not for only one project!

Solution 34 - Javascript

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

Solution 35 - Javascript

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!

Solution 36 - Javascript

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

Solution 37 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionistiyak ahamed MilonView Question on Stackoverflow
Solution 1 - JavascriptParth kharechaView Answer on Stackoverflow
Solution 2 - JavascriptHashan GunathilakaView Answer on Stackoverflow
Solution 3 - JavascriptMohit RathodView Answer on Stackoverflow
Solution 4 - JavascriptHappyHands31View Answer on Stackoverflow
Solution 5 - JavascriptcaseyjustusView Answer on Stackoverflow
Solution 6 - JavascriptKuldeepView Answer on Stackoverflow
Solution 7 - JavascriptMuhammad TahirView Answer on Stackoverflow
Solution 8 - JavascripteslotwinskiView Answer on Stackoverflow
Solution 9 - JavascriptttfreemanView Answer on Stackoverflow
Solution 10 - JavascriptsomtoView Answer on Stackoverflow
Solution 11 - JavascriptBishwajit VikramView Answer on Stackoverflow
Solution 12 - JavascriptKingswellView Answer on Stackoverflow
Solution 13 - Javascriptsaransh mehraView Answer on Stackoverflow
Solution 14 - JavascriptAndrew KoperView Answer on Stackoverflow
Solution 15 - JavascriptSohailView Answer on Stackoverflow
Solution 16 - JavascriptHenkeView Answer on Stackoverflow
Solution 17 - JavascriptAshPythonView Answer on Stackoverflow
Solution 18 - JavascriptyrkView Answer on Stackoverflow
Solution 19 - Javascriptthe_raging_deanerView Answer on Stackoverflow
Solution 20 - JavascriptbangashView Answer on Stackoverflow
Solution 21 - JavascriptJonathan HultView Answer on Stackoverflow
Solution 22 - JavascriptMab KianiView Answer on Stackoverflow
Solution 23 - Javascriptbeegee AssemView Answer on Stackoverflow
Solution 24 - JavascriptMr.spShuvoView Answer on Stackoverflow
Solution 25 - JavascriptdragonView Answer on Stackoverflow
Solution 26 - JavascriptMahdi SafarmohammadlooView Answer on Stackoverflow
Solution 27 - JavascriptzhulinpinyuView Answer on Stackoverflow
Solution 28 - JavascriptCuadoView Answer on Stackoverflow
Solution 29 - JavascriptWilliamView Answer on Stackoverflow
Solution 30 - JavascriptNEOdinokView Answer on Stackoverflow
Solution 31 - JavascriptBehram khanView Answer on Stackoverflow
Solution 32 - JavascriptIvan FerrerView Answer on Stackoverflow
Solution 33 - Javascriptuser2895965View Answer on Stackoverflow
Solution 34 - JavascriptJimmyTheCodeView Answer on Stackoverflow
Solution 35 - JavascriptTim HysniuView Answer on Stackoverflow
Solution 36 - JavascriptAnmolTheDeveloperView Answer on Stackoverflow
Solution 37 - JavascriptHamid ShojaView Answer on Stackoverflow