简体   繁体   中英

Why do I get an error: Cannot find module after downloading node.js?

I am having an issue with node.js, I installed Node, Homebrew and Xcode but when I type in the terminal: node index.js I get this error: Error: Cannot find module '/Users/traviskerr/index.js' at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15) at Function.Module._load (internal/modules/cjs/loader.js:562:25) at Function.Module.runMain (internal/modules/cjs/loader.js:829:12) at startup (internal/bootstrap/node.js:283:19) at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3) MacBook-Air-2:~ traviskerr$

Please help me figure out what went wrong and what I can do to fix it! I am very new to this and haven't got a clue of what went wrong. Much appreciated, thanks!

I have installed Homebrew and Xcode because apparently those are needed for Node.js however this does not seem to be working properly.

The expected result should say: Hello World on the next line but it is giving me

`Error Cannot find module '/Users/traviskerr/index.js'
at Function.Module._resolveFilename 
(internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
MacBook-Air-2:~ traviskerr$`

instead...

This is more than likely because you're index.js file doesn't exist within the current directory that you're running the command node index.js

Regardless of windows/mac, the following steps should get your file to run (assuming your index.js code is correct)

  1. Open a new terminal
  2. Navigate to the directory where your index.js is by running cd <path_to_index_directory>
  3. Run node index.js

If you're in the correct directory, running lst should list the file. If you don't see your file in the list, you're not in the correct directory (thus resulting in module not found)

In vscode explorer, give a right click in your project name and click on the option "Reveal in Explorer".

This is your project folder and you must to run node index.js inside your project folder , opening a terminal in this folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM