nodejs module.js:340 error: cannot find module

159,402

Solution 1

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.

Solution 2

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.

Solution 3

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,

Solution 4

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

Solution 5

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

Share:
159,402

Related videos on Youtube

Laura Martinez
Author by

Laura Martinez

Updated on July 09, 2022

Comments

  • Laura Martinez
    Laura Martinez almost 2 years

    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
      TheHippo over 10 years
      The part with npm install -g express does not make sense at all.
    • TheHippo
      TheHippo over 10 years
      From the error message I would suggest you misspelled the file name on your terminal / console.
    • ming_codes
      ming_codes over 10 years
      Indeed, one is testNode.js and the other is nodeTest.js?
    • Laura Martinez
      Laura Martinez over 10 years
      Nope. Thanks for the catch, but is not that.
    • Laura Martinez
      Laura Martinez over 10 years
      I did the misspelled when I was writing the question here, sorry; my bad. When I run it in the console I copied and pasted the name of the file.
    • Laura Martinez
      Laura Martinez over 10 years
      TheHippo thanks for your comment; I'm new with nodejs. I read on several other responses, to questions similar to this one, that running the install is the recommended thing to do.
    • SheetJS
      SheetJS over 10 years
      Did you use the node.js command prompt link or did you run cmd directly?
    • Prasanna Aarthi
      Prasanna Aarthi over 10 years
      I also got the same error please check your javascript file extension.. it should resolve your issue..
  • Laura Martinez
    Laura Martinez over 10 years
    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
  • shmuli
    shmuli over 10 years
    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.
  • Laura Martinez
    Laura Martinez over 10 years
    I'm still getting the fail message. > > .load c:/users/laura/desktop/testNode.js > Failed to load:c:/users/laura/desktop/testNode.js
  • Bailey Parker
    Bailey Parker over 8 years
    You're correct with regards to the question asked. However, it is a really bad idea to check in node_modules. 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.
  • Dana Woodman
    Dana Woodman over 8 years
    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.
  • Ricardo
    Ricardo about 6 years
    I used the first two commands (Remove node_modules/Reinstall packages) and works liek a charm. Thank you!
  • mishsx
    mishsx over 4 years
    Please improve your reply by adding code snippets, explanations that would resolve issue.