[node.js] nodejs module.js:340 error: cannot find module

I installed nodejs in C:\Program Files (x86)\nodejs

then I created a .js file and saved it in my desktop just to output 'hello world' in the console:

console.log('hello world');

When I tried to run the file from the command prompt:

C:\Users\Laura>cd desktop
C:\Users\Laura\Desktop>node nodeTest.js

I get:

module.js:340
throw err;
      ^
Error: Cannot find module 'C:\Users\Laura\Desktop\testNode.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3

I read many other related questions, and some of them recommend to run the install, and so I did.

C:\Users\Laura>npm install -g express

But no luck, still getting the same error message.

This question is related to node.js module

The answer is


While i tried to run my first node application i had encountered the same issue but when i tried to see what the issue might be it was the path of the file. I was running the node .js from different location and hence encountered the issue. When i fixed the path this worked fine.


I think the issue is the NODE_PATH environment variable. I had the same problem but once I issued (on Windows)

set NODE_PATH=C:\Users\MyUser\node_modules

then all worked ok.

Cheers,


I also got this issue and this was due to wrong path that we mention while running. Check your file path and also make sure that there is no space between the name of your directory name.


I executed following command and it works for me.

PM> npm install ee-first [email protected] node_modules\ee-first

In my case, i got this error because i was just in the wrong directory. So node couldnt find the module that i wanted to run. Just be sure your node file is in your Desktop directory.


I had the same problem, but finally I tried copying the Node_modules folder in the same project folder, and it worked.


EDIT: This answer is outdated. With things like Yarn and NPM 5's lockfiles it is now easier to ensure you're dependencies are correct on platforms like Heroku

I had a similar issue related to node_modules being modified somehow locally but the change was not reflect on Heroku, causing my app to crash. It's relatively easy fix if this is your issue:

# Remove node_modules
rm -fr node_modules

# Reinstall packages
npm i

# Commit changes
git add node_modules
git commit -m 'Fix node_modules dependencies.'
git push heroku master

Hope that helps for others with a similar issue.


Faced the same problem while trying to run node-red.

node <directory structure where js  is located>/red. js

In my case it was :

node AppData/Roaming/npm/node_modules/node-red/red.js

Restart your command prompt and check your path variable (type: path). If you can't find find nodejs installation dir from output add it to the path variable and remember to restart cdm again...


Try npm install --production and then npm start.


If you are using a framework like express, you need to put the package.json file into the folder you are using and don't forget change main name.


Easy way for this problem

npm link e

I had the same problem then I found that I wasn´t hitting the node server command in the proper directory where the server.js is located.

Hope this helps.


Try typing this into the Node command-line environment:

.load c:/users/laura/desktop/nodeTest.js.

It should work for what you're trying to do.

If you want to call the file directly, you'd have to have it in the root directory where your Node installation resides.


Make sure you saved the file as JavaScript. Un check 'Hide extensions for all known type' check box in Folder Options window will show you the correct file extension(Folder>>view>>Option).


I was having the same problem with a server someone had written a while back. It turns out I didn't have a few things installed that the script required.

This question was helpful to me.

I ended up being able to use:

npm install yourMissingDependency 

Although in your instance it may just be a file that it doesn't have a path to. You could try putting that file in the same folder you are running the node script from.


I faced the same problem. I just copied the testNode.js file(that contain the test code) and pasted into the root of nodejs directory manually. I tried this command C:\Program Files (x86)\nodejs>node testnode.js Bingo! I received this message.

enter image description here

Then I typed this url in a browser and received the message "Hello World". Hope this help somebody.


  1. Try npm start in Node.js Command Prompt.
  2. Look at the end of the messages - it gives you the path of log file in "Additional Logging Details ..." something like c:\users\MyUser\npm-debug.log
  3. Open this file in Notepad and find the real address of Node.exe : something like C:\\Program Files\\nodejs\\\\node.exe
  4. Try cd to this path
  5. Call node.exe + <full path to your server file.js>

    Server is listening on port 1337 !
    

You need to:

  • Remove the tick of Hide extensions for known file types from Windows Explorer folders option
  • Your file will appear as testNode.js.txt
  • Remove the trailing .txt so as the file to be resolved as JS file

That's it, now it works!


Hi fellow Phonegap/Cordova/Ionic developers,

  I solved this issue by doing the following

  1. C: drive -> Users -> "username" eg. john -> AppData -> Roaming

  2. Inside the "Roaming" folder you need to delete both "npm" and "npm-cache" 
       folder.

  3. Now build your project, and it should work

Happy coding!!!


I had a nearly identical issue, turned out my JS file wasn't actually in the folder I was calling it from, and I had gone one folder too deep. I went up one directory, ran the file, it recognized it, happily ever after.

Alternatively, if you go one folder up, and it gives you the same error, but about a different module, take that same file in your parent folder and move it into the subfolder you were previously trying to run things from.

TL;DR- your file or its module(s) is not in the folder you think it is. Go up one level