I\'m getting following Console Error.
Error : Cannot find module
Here is the full error i\'m getting in console. What should I do?
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.
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'
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.
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
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.
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!