43

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.

TheHippo
  • 61,720
  • 15
  • 75
  • 100
Laura Martinez
  • 469
  • 1
  • 4
  • 4

22 Answers22

33

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.

Dana Woodman
  • 4,148
  • 1
  • 38
  • 35
  • 9
    You're correct with regards to the question asked. However, [it is a really bad idea to check in `node_modules`](http://stackoverflow.com/a/19416403/568785). Although Heroku recommends it, the Node.JS buildpack will automatically run `npm install` on deploy. There's no need to check code into your repository that isn't yours and then lump a bunch of atomic commits/changes to your dependencies into one commit. – Bailey Parker Aug 03 '15 at 05:26
  • 1
    I wrote this back when the recommended approach was to version control mode_modules, but yes for anyone reading this now try to avoid version controlling. – Dana Woodman Aug 03 '15 at 15:07
  • I used the first two commands (Remove node_modules/Reinstall packages) and works liek a charm. Thank you! – Ricardo Feb 11 '18 at 19:35
21

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.

Community
  • 1
  • 1
misterkeeter
  • 223
  • 1
  • 4
6

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,

Jacek
  • 1,048
  • 15
  • 21
3

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...

OptimusC
  • 31
  • 1
3

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

Eleanor Zimmermann
  • 414
  • 1
  • 8
  • 26
2

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.

shmuli
  • 5,086
  • 4
  • 32
  • 64
  • Thanks @Shmuli for your comment. As TheHippo and lightblade pointed out, when I submit my question I did a typo. The actual name of the file is testNode.js. Anyway, I tried your suggestion using the correct file name, but didn't work. I typed in: > .load .c:\users\laura\desktop\testNode.js I got: Failed to load:.c:\users\laura\desktop\testNode.js – Laura Martinez Sep 24 '13 at 01:47
  • Oh, sorry @Laura Martinez. You can do it like this instead: `.load c:/users/laura/desktop/testNode.js`. (had to take out the period before `c:/`...) I tried this and it works. Glad you figured out that it was a typo issue... I went back and corrected my answer. – shmuli Sep 24 '13 at 02:16
  • I'm still getting the fail message. > > .load c:/users/laura/desktop/testNode.js > Failed to load:c:/users/laura/desktop/testNode.js – Laura Martinez Sep 24 '13 at 02:29
2

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!!!

Ariven Nadar
  • 1,278
  • 13
  • 13
1

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.

mbkh10
  • 43
  • 1
  • 11
1

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!

Basim Hennawi
  • 2,651
  • 3
  • 19
  • 31
0

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.

Ricardo Fornes
  • 382
  • 1
  • 4
  • 11
0

Try npm install --production and then npm start.

0

Easy way for this problem

npm link e
trquoccuong
  • 2,857
  • 2
  • 20
  • 26
0

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.

Bilal Ahmed Yaseen
  • 2,506
  • 2
  • 23
  • 48
0
  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 !
    
carloabelli
  • 4,289
  • 3
  • 43
  • 70
0

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

George511
  • 1
  • 1
0

I executed following command and it works for me.

PM> npm install ee-first ee-first@1.1.0 node_modules\ee-first
Najera
  • 2,869
  • 3
  • 28
  • 52
0

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.

0

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.

B.G.
  • 141
  • 1
  • 1
  • 9
0

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.

0

Remove the fs extra from the path -

  path: 'D:\\FrontEnd Dev\\Git projects\\Appname-master\\node_modules\\webpack-manifest-plugin\\node_modules\\fs-extra\\package.json',
  requestPath: 'fs-extra'

And run the command npm i

Jakub Kurdziel
  • 3,216
  • 2
  • 12
  • 22
Sanket Shelke
  • 101
  • 1
  • 2
-1

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).

Dennis Jose
  • 1,589
  • 15
  • 35
-1

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
Siena
  • 778
  • 10
  • 23