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)
This question is related to
javascript
node.js
node_modules
directory package-lock.json
file npm install
npm start
OR
rm -rf node_modules package-lock.json && npm install && npm start
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
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.
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.
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.
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).
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
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'
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
Hashan Gunathilaka was correct. this strange behavior exists. I just deleted the duplicates and closed the command prompt to open again in windows. Worked fine.
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
.
For me "npm install" again from command prompt worked. Command Prompt must be "Run as Administrator"
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.
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.
same happen to me i just resolved by deleting "dist" file and re run the app.
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.
you need start server use follow command
npm start
or
yarn start
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.
The below commands resolved the issue for me.
npm install node-gyp -g
npm install bcrypt -g
npm install bcrypt -save
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.
I changed name of my Project's folder and It's worked , i don't know why :)
This error message is easy to reproduce.
cmd
, Enter.
On Linux: Ctrl + Alt + t.)npm
and hit Enter to see if Node.js is installed.command not found
, download at https://nodejs.org/en/download/
and install.sudo apt install nodejs
if you prefer.)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!
Create the .js file inside the main directory not inside the sub folders such as public or src.
it finally worked for me after I did sudo npm i cjs-loader (and make sure to install express, not just express-http-proxy)
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 . "
},
Simply type "node NodeJsProject/app.js"
Your program will run :)
I was facing same problem.. I downgraded webpack-dev-server to 2.5.1 version. Now everything is working fine .
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.
Source: Stackoverflow.com