0

Just opened my IDE for my project on react and this weird stubborn error popped up on my nodemon server.

node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module 'C:\Users\Hammad\ngo_project\index.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...

I tried deleting and reinstalling node_modules folder and even package_lock but still the same error is popping.

Hammad Malik
  • 9
  • 1
  • 1
  • 2
  • Please share the file structure or package.json – Bozhinovski Dec 14 '22 at 12:49
  • Which way you are using for file imports `require` or `import`? And if you are using `import` syntax do you have `"type": "module"` (ES 6 modules) enabled in your package.json? – sha'an Dec 14 '22 at 13:55

2 Answers2

1

You will execute the node index.js command
Want to launch a file with the name index.js
You are in the folder of your project, you have this path: C:\Users\Hammad\ngo_project
Your file is not in this folder, maybe you have a folder called "js" or some other name that contains your index.js file.
When you launch the node index.js command.
Node Js is looking for your file in the project C:\Users\Hammad\ngo_project Node Js does not go to other subcathologists, therefore it does not find your index.js file.
You need to move the index.js file to your ngo_project folder.
After that, repeat the launch of the node index.js command.

Also important!
If you have "type": "module" in package.json
You need to use in your index.js connection like this: example import fetch from 'node-fetch';
If "type": "CommonJS" is specified in the package.json file or not specified at all.
You need to use in your index.js connection like this: example const fetch = require('node-fetch');
Good luck!

Brendan8c
  • 369
  • 2
  • 11
-2

Try use Yarn, not npm. I use Yarn and it help me.

Delete node_modules and package-lock.json, after that in terminal write yarn install