0

I installed Node.js (LTS version) via their website on my Windows, created a new folder called "mynode" and a text file inside this folder called "hellonode.js, opened the command prompt and changed to the directory "mynode" but when I run node hellonode.js, I get the "Cannot find module" error.

C:\Users\Mina>cd mynode

C:\Users\Mina\mynode>node hellonode.js
node:internal/modules/cjs/loader:1080
  throw err;
  ^

Error: Cannot find module 'C:\Users\Mina\mynode\hellonode.js'
←[90m    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)←[39m
←[90m    at Module._load (node:internal/modules/cjs/loader:922:27)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)←[39m
←[90m    at node:internal/main/run_main_module:23:47←[39m {
  code: ←[32m'MODULE_NOT_FOUND'←[39m,
  requireStack: []
}

Node.js v18.16.1

I already searched for solutions and I tried some suggestions:

Frist I deinstalled Node.js and reinstalled it, but nothing changed.

As suggested here: "internal/modules/cjs/loader.js:582 throw err" I ran npm init so that a "package.json" file was created (I left everything as in the default settings.) and then npm install and afterwards npm start, but this got me the "Missing script: "start"" error, so that I added "start": "node hellonode.js" under "scripts" in the "package.json" file, and run npm start again, but this again got me the same main error from above: Cannot find module 'C:\Users\Mina\mynode\hellonode.js'.

What else can I do? Am I missing a step?

Here you can see the files in the path:

Verzeichnis von C:\Users\Mina\mynode

03.07.2023  18:16    <DIR>          .
03.07.2023  18:16    <DIR>          ..
03.07.2023  18:01               593 hellonode.js.txt
03.07.2023  18:17               199 package-lock.json
03.07.2023  18:16               202 package.json
               3 Datei(en),            994 Bytes
               2 Verzeichnis(se),  3.924.852.736 Bytes frei
Mina
  • 117
  • 7

1 Answers1

0

You are givind wrong file to node that's why it shows error.

You have to rename your file name by hellonode.js instead of hellonode.js.txt then try

node hellonode.js
vatsal mangukiya
  • 261
  • 2
  • 14
  • But the file is called "hellonode.js", using `dir` in the command prompt to display all files in a directory just shows it as "hellonode.js.txt" as it is still a text file. Is a text file saved with ".js" not valid to use in Node.js? – Mina Jul 03 '23 at 17:00
  • You have to save file name with `.js` extension only. Example: `index.js` ✅ Valid `index.js.txt` ❌ InValid it will not parse by `node.JS` – vatsal mangukiya Jul 03 '23 at 17:02
  • That is exactly what I did. It is called "hellonode.js", not "hellonode.js.txt", it just shows as that in the command prompt but not in the folder itself. – Mina Jul 03 '23 at 17:03
  • Issue solved?? If not then give me output of `dir` command – vatsal mangukiya Jul 03 '23 at 17:04
  • Alright, I just had renamed the file after closing my text editor, so the file was saved as a .txt file. With the "save as" option the file was correctly saved as a .js file. Thank you! – Mina Jul 03 '23 at 17:19
  • @Mina I think enabling file extensions in Explorer will help... – GmBodhi Jul 04 '23 at 01:50