nodejs module.js:340 error: cannot find module - node.js

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.

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.

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

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

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

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.

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 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.
Then I typed this url in a browser and received the message "Hello World".
Hope this help somebody.

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!

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 npm install --production and then npm start.

Easy way for this problem
npm link e

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.

Try npm start in Node.js Command Prompt.
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
Open this file in Notepad and find the real address of Node.exe :
something like C:\\Program Files\\nodejs\\\\node.exe
Try cd to this path
Call node.exe + <full path to your server file.js>
Server is listening on port 1337 !

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

I executed following command and it works for me.
PM> npm install ee-first ee-first#1.1.0 node_modules\ee-first

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.

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.

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.

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

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

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

Related

how to fix "Error: Cannot find module '/home/container/index.js'", I've tried everything

So the host that I use for my discord bot died so I downloaded all the code and tried hosting it on my PC. But I get the following error.
Error: Cannot find module '/home/container/index.js
I've tried everything I can:
updated nodejs
reinstalled all my packages
Changed package.json
How can I fix this?
Cannot find module means you are trying to run non-existent file.
Ensure there is index.js in your working directory. If not,
Use cd to go to the directory where it is (usually it's src near the package.json)
Run node ./index.js
Many time people have just the problem of command line to run file,
as windows and linux have
different syntax to run .js file
i.e. node ./index.js for linux
and node .\index.js for windows

Node.js node:internal/modules/cjs/loader:936 error while trying to use discord.js

I'm trying to make a discord bot with the command prefix "*" but when I try to get the bot online/run it, it gives me this error:
node:internal/modules/cjs/loader:936
node:internal/modules/cjs/loader:936 throw err; ^
Error: Cannot find module 'C:\Users\name\const'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] }
The module I'm using is discord.js and i've installed discord.js on CMD and the VSC terminal but it still comes up with the MODULE NOT FOUND error. I've looked around the internet and I can't find one that fixes the problem. I've tried reinstalling Node.js but it won't stop coming back. Please help, Thanks!
same error with me but I just change file dir cause I run node <filename.js> in the wrong dir so I just change like ---
the first step chacks you are in the right dir or not type: > ls
then if you are in the right dir then type: node <filename.js> if not then change your dir to change dir type: cd <filename>
and then run your first app or file for run type: node <filename.js>
I hope your error is solved!
you need to run it as an administrator so sudo(whatever you tryna run) as long as you are in the right directory
On Windows I solved it like this.
I went to the folder: C:\Users\Documents\AppData\Roaming\npm
And I deleted the file. create-react-app.
I started the project again, and it worked.
root cause, my path was messed up, but I only figured that out once I found where my npm packages where. I had thought they were in ../Programs/Roaming/npm etc but since an update (I think to node) my packages were actually in a Python Lib subdir.
Your Answer might be the same as mine, and I found out all this by using the
npm list -g
Command - which showed the globally installed packages, but also highlighted that this was nowhere to be found in my path
Hope this helps someone!
I had the same error and it was a case of trying to run the file in node from the wrong location.
My solution was to use the terminal and CD into the exact folder the file I was trying to run were and then after that I could run node filename.js.
In my case (this error ocurred at running docker container of next app), it was because I was running docker-compose without the flag --build, but of without copy lock files (package-lock.json and/or yarn.lock).
I updated my Dockerfile:
[...]
COPY yarn.lock .
RUN yarn install --frozen-lockfile
CMD [ "yarn", "dev" ]
And after, run with docker-compose up --build
When I was following a JS tutorial i got this error message too.
The problem was I copied an existing JS file and renamed it but it
through that error when i ran it.
When i created a new JS file, and copied the code in, and ran, it worked.
Before starting your server you need to make sure that the start file is mentioned below
cross-env NODE_ENV=production node index<start file name eg:index for index.js>
It happens when you run npm run start without getting into the project first... So, before starting to compile your project, run this command: cd <project folder name>...
It will stop showing the "Missing Module" Error and smoothly run your desired output in your browser
When was trying to find 'npm' & 'Rdme' path from command prompt to update the Rdme was getting this error Log
This error is caused when file can't be found or its a wrong path, In my case I added the missing file to respective folders- configured & updated in Environment variables (after adding missing files)
For the coming next. For me I'v got the error when I tried to run a package.json script with the npx run rather npm run.
In my case, I used chocolatey to install nodejs and it fixed my issue.
Open an Administrator Command Prompt (right click Command Prompt and select "Run as Administrator"), then run the following command:
choco install -y nodejs-lts
I hope it works for you too :)
The error tells that you've required packages that you haven't installed. So please make sure that you've installed every package that you've required in your bot. Also if you're sure that you've installed it, please check it again, there were namely some problems for some people that some of their packages got uninstalled while updating Discord.js to version 13.
For your problem in the comments ("TypeError [CLIENT_MISSING_INTENTS]"), it is now required in Discord.js version 13 to add your needed intents in the client. Please read this part of the Discord.js guide.
I fixed this issue by changing the opened directory of the terminal. Just go to the terminal and type: cd <folder name> until you reach the folder in which your js file is located. Then save and run again.
npm uninstall webpack-cli -D
npm install webpack-cli#3.3.12 --save-dev
than work fine

Reading /Users/myuser/Code/OLD_PROJECT_NAME/node_modules/devtron/manifest.json failed

I recently set up a new project based on https://electron-react-boilerplate.js.org. This worked fine for a little bit, then I decided to start over again and re-cloned the electron-react-boilerplate Git repo to a new directory and deleted the old one.
Now when I run yarn run dev in the new project directory, I get a strange error:
Reading /Users/myuser/Code/OLD_PROJECT_NAME/node_modules/devtron/manifest.json failed.
Error: ENOENT: no such file or directory, open '/Users/myuser/Code/OLD_PROJECT_NAME/node_modules/devtron/manifest.json'
at Object.fs.openSync (fs.js:577:3)
at Object.module.(anonymous function) [as openSync] (ELECTRON_ASAR.js:166:20)
at Object.fs.readFileSync (fs.js:483:33)
at Object.fs.readFileSync (ELECTRON_ASAR.js:563:29)
at getManifestFromPath (/Users/myuser/Code/NEW_PROJECT_NAME/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/browser/chrome-extension.js:30:26)
at Function.BrowserWindow.addExtension (/Users/myuser/Code/NEW_PROJECT_NAME/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/browser/chrome-extension.js:362:22)
at Function.BrowserWindow.addDevToolsExtension (/Users/myuser/Code/NEW_PROJECT_NAME/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/browser/chrome-extension.js:394:40)
at App.<anonymous> (/Users/myuser/Code/NEW_PROJECT_NAME/node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/browser/chrome-extension.js:423:23)
at Object.onceWrapper (events.js:273:13)
at App.emit (events.js:187:15)
Note that for some reason, it tries to read the manifest.json file from the old project directory which doesn't even exist anymore. I've grepped the entire new directory for occurrences of the old project name hunting down what could be wrong but nothing comes out of it.
I've tried removing node_modules, didn't help.
I also tried running yarn cache clean but that didn't help either.
Where could it possibly be getting the old project directory from?
I'm on macOS Mojave with Node 11.4.0.
I experienced the same problem after experimenting with Electron React Boilerplate (ERB).
There's a user-wide Electron configuration file with the name DevTools Extensions which stores folder paths related to Chrome DevTools. The file can be found at the following locations:
Linux: ~/.config/Electron/DevTools Extensions
Mac: ~/Library/Application Support/Electron/DevTools Extensions
Windows: %appdata%\Electron\DevTools Extensions
Mac path as per agmangas' comment, Windows path as per ekundayo's
answer.
In my case, the file contained the path into my old/experimental ERB project, i.e. the path at the top of the error message.
Deleting the offending path from this file solved the issue for me.
I had the same problem before. Highly possible that you already have another electron instance running in the background. Start activity monitor and get rid of all running electron instances then try again.
I experienced same problem also and #snwflk's answer worked for me, but on windows. In solving it, I had to navigate to the application data directory like %appdata%\Electron and deleted all the contents of the folder, then proceeded to rebuild the application again and voila! it worked.

node jitsu cannot find local modules

I have an application successfully working locally so I know the code works. However when I go to deploy to node jitsu I get an error that it cannot find a local module. Here is what I have:
File Setup:
/index.js
/config/config.js
index.js
var cfg = require('./config/config.js');
When trying to deploy node jitsu is giving me an error:
Error: Cannot find module './config/config.js'
Since all this code works locally I do not believe this is a coding issue. I am under the impression that local modules do not need to be included in package.json but perhaps they do for node jitsu? I read their documentation but cannot find anything special for local modules.
Thanks!
Local modules like this should work properly.. so long as you don't have it in .gitignore or .npmignore.
Modules in the node_modules directory require that you add it to the bundledDependencies array in your package.json file.
An easy way to check for whether the file is included in your deploy is to run tar -tf $(npm pack).
I had this exact same error on deploy, but caused by a different root cause. In case anybody stumbles into the same problem:
File Setup:
/public/Data/TargetData.js
app.js require statement:
var target = require('./public/data/TargetData.js');
My local Mac OSX environment allowed the capitalization difference of /data/ vs. /Data/ - the Nodejitsu server did not.

Error code: 800A1391 Source: Microsoft JScript runtime error Running Grunt - Module is undefined

New Grunt user here who is using a lot of new tools (npm nodejs) today.
I've got Grunt "installed" and have been able to create a grunt.js file using the init task as described here: http://net.tutsplus.com/tutorials/javascript-ajax/meeting-grunt-the-build-tool-for-javascript/ and here: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md. But whenever I run the "grunt" command I get an error:
Windows Script Host
Script: c:\users\[]\Documents\code\grunt\grunt.js
Line: 2
Char: 1
Error: 'module' is undefined
Code: 800A1391
Source: Microsoft JScript runtime error
As explained in the FAQ, you need to type grunt.cmd instead on Windows because the OS tries to launch grunt.js
Or you can install grunt-cli globally instead. This package will run any version of Grunt if it's been installed locally to your project.
SOLVED !!
So, this problem occur because windows by default associative < *.js > files
with >>
"Microsoft Windows Based Script Host".
grunt need to open by default with (grunt.cmd).
it easy to slove, by change default app (open with..)
Guide :
Go to any javascript file with "js" extension. (any file)
Right-Click(mouse) > Properties > "Opens with:" Change...(button)
Choose Notepad ( or any javascript IDE ).
PROBLEM SOLVED ! :)
good luck
If you're getting a "Microsoft JScript runtime error" that means that node.js isn't even getting invoked; instead Windows Script Host is trying to run your code. That's probably a problem with filetype associations; IIRC Windows defaults to trying to execute a ".js" file with WSH. You may wind up having to create a shortcut to your script, specifying a command line (probably something like "node %1") and a starting directory in order to make sure that it's executed properly.
It would help if you could tell us exactly how you're trying to invoke your code.
it seems that in the latest versions of the grunt modules, you would have to do the following to have it work under windows:
remove any globally installed grunt
npm uninstall -g grunt
install grunt-cli globally
npm install -g grunt-cli
install grunt locally into your project
npm install grunt
installing grunt (v0.4.x) globally does not seem to create the necessary grunt.cmd anymore. it seems that the recommendation is now to have grunt installed locally to be able to use version-specific Gruntfiles
As Florian F suggested, running grunt.cmd works. This is because of the process Windows is looking for your grunt command.
When typing grunt -h Windows will proceed to look for the following files:
./grunt.cmd
./grunt.* (grunt.js is found in this case which is why you see "module is undefined")
%APPDATA%/npm/grunt.cmd
An alternative to using "grunt.cmd" is to use grunter which simply renames the command to grunter... then you no longer have this problem.
To answer this, first we need to understand that the error is caused because it is being executed by Windows Script Host.
Now, run the code from your cmd promt with the following syntax:
>node <application_name>.js
this will allow the Node.js application to open through V8 JavaScript engine(Google's).
P.S: Please reply back if this has helped in resolving your issue else post the problem you are facing after trying this.
I had a similar issue, the problem is file association, I would recommend:
right click on a .js file and choose open with.
then you choose nodejs/node.exe (somewhere in "program files" folder
then make tick box where it says "always open .js files " (paraphrasing)
That should do the trick.
I went through the same issue when running an old Node project.
The issue was with the name of the js file, it was node.js. So the while running the command node node.js, it was opening up a windows dialogue box.
I just changed the name of the file to app.js and the error flew away.
So, in my case i had tryed all the mentioned above with no result.
But i have fund that im dont type: node in the full sentence as the following snipet
node script.js.And remember never understimate your own miscoding.
Solution:
Go to any javascript file with "js" extension. (any file)
Right-Click(mouse) > Properties > "Opens with:" Change...(button)
Choose Notepad ( or any Javascript IDE like VS Code ).

Resources