Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs module.js:340 error: cannot find module

Tags:

node.js

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.

like image 669
Laura Martinez Avatar asked Sep 23 '13 23:09

Laura Martinez


People also ask

How do I resolve Cannot find module error using node JS?

To solve the "Cannot find module" error in Node. js, make sure to install the package from the error message if it's a third party package, e.g. npm i somePackage . If you get the error with a local module, make sure to point the node command to a file that exists.

Can not find module in node JS?

To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. js or index. js file. or delete the node_modules folder and package-lock.

Can not find module Express error?

To solve the error "Cannot find module 'express'", install the package by running the command npm install express in the root directory of your project. If you don't have a package. json file, create one by running npm init -y . The error occurs when we try to import the express package without installing it.


1 Answers

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.

like image 95
Dana Woodman Avatar answered Oct 15 '22 14:10

Dana Woodman