I got path error : [module not found] whenever I try to run any npm related command in VS Code - node.js

Whenever I try to run any npm related command such as npm -v or npm install , it gives me the error:
From the screenshot, you can see that it is trying to navigate to find the module but the module can not be found at:
C:\Program Files\nodejs\node_modules\npm\bin\node_modules\npm\bin\npm-cli.js
Actually, the location of the npm-cli in my system is:
C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
In fact, I have set the path in the environment variable like this:
User variables:
System variables:
The solution I got is to run this command every time I want to run any npm related command:
SET PATH=C:\Program Files\Nodejs;%PATH%
But it is not a solution to run this command in the terminal every time I start working on my project in VS Code. I want to permanently set the path in the environment variable.
How can I resolve this?

Same issue, this was first major post I saw, so adding my fix for others who find themselves here. Hope it's helpful:
Restart PC, open VS Code, and delete any terminal instances that are still open, as they will not inherit the new path(s) if they are 'session-restored'.
My root cause was updating Node, which for whatever reason prefaced an environmental variable in $PATH$ with a semi-colon ';'. Worked in external cmd.exe, but didn't work in VS Code integrated terminal until I realized I had to restart the old ones.
This thread was the one that eventually got me there:
VS Code terminal doesn't recognize PATH variables

Related

How to run NodeJS CLI tools without having to type npx

TL;DR
I cannot execute commands such as tsc unless I include npx before it (e.g npx tsc). How can I fix this?
The title is a bad explanation of the problem I have.
Say I have installed an npm package with a CLI with it (typescript in this example). And the CLI is used like
tsc <filename> or just tsc. But whenever I try to do it like that, I get an error like
'tsc' is not recognized as an internal or external command,
operable program or batch file.
But... when I do
npx tsc
then it works!
So whats the problem?
The problem with doing npx tsc is because
npx is slow at executing commands
its annoying having to type npx and the front of every command.
And the thing is, this was originally not a problem with WSL.
Why dont you just use WSL?
I have always had problems with WSL (primarily permission issues due to security reasons) and so I uninstalled WSL and just used command prompt. I would have perferred using WSL but it was simply not an option.
Other Info:
I am using Windows command prompt.
I have installed the packages globally
So is there a way to just execute commands that way or is it Command prompts fault?
! this only works for Windows !
Ok, so I came across this post and thankfully, the first answer there was the solution!
Just add %USERPROFILE%\AppData\Roaming\npm to the path variable in system variables!
To access the system variables, press the Windows key, type Environment variables and click on Environment variables at the bottom of the window. The path variable can be found under User variables for (profile name).

Node & Npm not recognized by WebStorm's terminal

I have paths to Node and NPM in both my user defined environment variable PATH and in system variable Path.
For node: C:\Program Files\nodejs
For npm: C:\Users\Username\AppData\Roaming\npm
Now node and npm are recognized by Windows Command Prompt but not by WebStorm's terminal. I get error:
'node' is not recognized as an internal or external command
Does anyone know what is problem? I could use WebStorm's terminal normally till today, but something went wrong and now I'm stuck.
EDIT:
echo %PATH% screenshot:
Settings/Tools/Terminal
screenshot:
I had similar issue. I had to select File -> Exit from the IntelliJ application as opposed to using IntelliJ's internal restart option: File -> Invalidate Caches / Restart -> Just Restart.
I had the similar problem like this.I was working with my project with another computer. and after that i decide to work with this project with my computer.so after moving the Files of project. i installed the Node.js and i had all of the paths about Node but when i was want to Run the project. i had this problem. i search a lot but nothing works. so i guess maybe when i restart my computer the new paths of variable system will work well. i restarted and my problem solved!
For those who are encountering this problem when you try to edit the run configuration, you have to put "start" as the command instead of "npm". By default Webstorm knows it's a npm project so it already starts with npm, you simply have to put the start command after that.

Browser-sync command not found even after setting the correct path to the environment variable

I am facing a problem which i found some solutions but it is still not working.I installed nodejs for windows 32 bit version, and everything went well.Now when i installed browser-sync via command line running the command
$ npm install -g browser-sync
Now everything got installed correctly with a couple of warnings. Now when i ran the command
$ browser-sync --version
it said browser-sync command not found.So i googled and found out that my environment variable might not be set correctly so it is not picking up my command. So then i tried finding the correct path to be set in the env variable via the following command which gave the result
$ npm bin -g
C:\Users\TEMP\AppData\Roaming\npm;npm\npm
(not in PATH env variable)
So i tried adding the path via the command
$ PATH %PATH%;C:\Users\TEMP\AppData\Roaming\npm;npm\npm
But it is still not working.I checked whether the path is actually added directly from the environment editor and via running the command $ Path,and it is actually adding the path but the problem still persists.
I think the problem is with the folder name which has semicolon in it "npm;npm" which is actually used to separate paths.I am not quite sure why it is adding the folder with such a name, i tried renaming it but as soon as i run the command npm bin -g it makes a new folder with the same name.
Also i tried using the short name for the folder which was "NPM_NP~1" to avoid that semicolon issue but it is still not working.
I am stuck with this since past 2 days and hopefully i can find a solution from you guys so i can get on with some actual development work. I gave you guys as much info as i could. Let me know if you need something more and specific.
You need to have NODE_PATH set:
$ set NODE_PATH=%AppData%\npm\node_modules

Node JS NPM modules installed but command not recognized

Node JS and NPM were working well before. Recently I have re-installed the Node JS, NPM and the problem started. After I install a module like an example npm install -g bower, the module gets installed successfully but bower -v gives
'bower' is not recognized as an internal or external command,
operable program or batch file.
I have checked the installation path
C:\Users\XXXXX\AppData\Roaming\npm\node_modules has all the old installed modules. I have tried to uninstall them and reinstall the modules, but still, I am getting the same error.
Even I have deleted the entire folder and installed all the modules again but the result is the same.
I don't know why I am getting this error after reinstalling NodeJS NPM.
I had this same problem and fixed it by adding the 'npm' directory to my PATH:
Right-click 'My Computer' and go to 'Properties > Advanced System Settings > Environment Variables'.
Double click on PATH under the 'User variables for Username' section, and add C:\Users\username\AppData\Roaming\npm obviously replacing 'username' with yours. Based on the comments below, you may need to add it to the top/front of your path.
Restart your console window or IDE and you should get a response from the bower command.
I had the same problem as well but installed it globally so the other answers didn't work.
The nodeJS install may not have added npm to your PATH so it's not recognised globally.
Firstly: To figure out if you installed globally or for current user
If there is an npm folder at C:\Users\(your username)\AppData\Roaming\npm you've installed it for the current user
If there is an npm folder at C:\Program Files\nodejs\node_modules\npm or C:\Program Files(x86)\nodejs\node_modules\npm you've installed it globally for all users to access
Secondly: To add it to the Path
Right-click My Computer
Click on properties down the bottom
Click on Advanced System Settings in the left bar
Click on Environment Variables down the bottom
Now depending on whether you installed globally or for the current user will determine which PATH variable you are updating and with what path location
Local
Double click on PATH under 'user variables' section
Add C:\Users\(your username)\AppData\Roaming\npm without the quotes
Global
Double click on PATH under 'user variables' section
Add C:\Program Files\nodejs without the quotes (or with (x86))
Finally
Close all terminals or programs that aren't able to find npm and open them up again
Run npm config get prefix and check the correct npm path first and append the output to the PATH using command or adding manually.
npm config get prefix
C:\mydev\tools\npm\npm
set PATH=%PATH%;C:\mydev\tools\npm\npm
If the package is successfully installed and still shows the message "'npm' is not recognized as an internal or external command, operable program or batch file."
Click windows start button.
Look for "ALL APPS", you will see Node.js and Node.js Command prompt there.
You can run the Node.js Command prompt as administrator and soon as its run it will show the message "Your environment has been set up for using Node.js 6.3.0 (x64) and npm."
You should probably check if your node/bin directory is in your PATH variable. Look for it where you've been installing these global modules.
I'm not on windows, so I can't tell you the exact command, but you can check these variables navigating to [Control Panel -> System -> Advanced -> Environment Variables] or something like this.
I had the same issue in Windows
Two solution worked for me
If you are using bash shell, use npm run
Example : $ npm run ng --version
Use Windows power shell or command prompt
In my case the npm folder was already in the PATH variable.
If after trying everything else, you also still can't make it work, try to delete the folder from the PATH variable and put it at the beginning of the list.

Grunt on Windows 8: 'grunt' is not recognized

I'm having a problem running Grunt from the command line on my Windows 8 machine.
My research indicates the most common solution is to install grunt-cli, since Grunt is no longer global. I also need to make sure I actually install the Grunt task runner, since that's not installed with grunt-cli.
Other solutions point to the PATH system environment variable, but that appears to be pointed as I'd expect to:
C:\Users[username]\AppData\Roaming\npm
Having done all that, I'm still getting a "'grunt' is not recognized as an internal or external command, operable program or batch file" error message in the CLI. I've tried the following things, uninstalling everything after every attempt:
Installed grunt-cli globally (npm install -g grunt-cli), then grunt at the directory level I want to use it (npm install grunt)
The same as above, but with the order of installation reversed
The same as both of the above, but using the Admin Command Prompt
Am I missing something obvious?
I've not had any issues with grunt on several different windows 8 machines.
If you open the folder: C:\Users\[username]\AppData\Roaming\npm
Do you have a file named grunt.cmd in this folder?
If not I'd maybe try npm install -g grunt-cli again, maybe from an elevated command prompt.
If this exists and you have C:\Users\[username]\AppData\Roaming\npm in your PATH environment variable then typing grunt from a command prompt should work.
Silly question, have you tried closing the command prompt and opening a new one?
Confirm your PATH is correct (and not messed up). Just type PATH from the command prompt. There's really no other explanation that makes sense given the error you're describing and the steps you've taken.
Normally, using the where grunt command would have found grunt.cmd in your path if npm is installed correctly and it has been properly added to the system path.
Close all Command Prompt instances.
Start a new Command Prompt instance.
Type PATH Enter and verify if C:\Users\Username\AppData\Roaming\npm is part of the path.
If not, you need to log off and on again,or close the Command Prompt and restart the explorer process.
In the Command Prompt, type where grunt Enter.
You're good if it reports:
C:\Users\Username\AppData\Roaming\npm\grunt
C:\Users\Username\AppData\Roaming\npm\grunt.cmd
Otherwise, you have to reinstall the grunt-cli package if it reports:
INFO: Could not find files for the given pattern(s).
Apparently, programs that change the PATH environment variable must broadcast a WM_SETTINGCHANGE message. The Windows' System settings window does it correctly when you change the PATH variable, but the NPM installer doesn't. That's why you have to restart explorer (or log off or restart, which has the same effect).
I know this has been answered but I thought I'd offer my step by step solution for windows 8.
First thing I checked was the PATH in my laptops Environment Variables (Right click my computer > properties > advanced system settings > Environment Variables)
It wasn't listed in there so I added a new variable in User variables (so it was specific only to my user account)
In the new user variable prompt I entered the following;
Variable Name: PATH
Variable Value: %USERPROFILE%\AppData\Roaming\npm
Quit command prompt, repoened, navigated to my projects directory and tried running grunt again and... SUCCESS!
I had the same issue.
I tried different things:
Restart computer
Deleted the grunt folder and ran
npm install -g grunt -cli
Didn't work.
Finally tried:
npm install -g grunt-cli
Worked perfectly.
Tried
where grunt
and I saw 2 locations where it was found.
I was facing the same problem on windows 8
I have added ' %APPDATA%\npm ' to the path variable . It has been working fine.
some times NPM install corrupts the basic windows path. i usually have a copy of my own version of PATH mainted separately. every week or on some installs i manually configure and update the %PATH% variable.
Basically Grunt.cmd is not availbe through %PATH% variable.
I have stucked with problem on Windows 8, that after install grunt-cli I've always got "command not found" while I'm tried to check grunt -v or where grunt. So I've added to enviroment PATH this path C:\Program Files (x86)\Git\local and run grunt.cmd from that folder (you need to look in node_modules folder here). And after reloading my terminal everything started to work.
Same happened to me and here was the solution:
Have you got 2 different versions of Node.JS installed?
Maybe Nodist?
This means you likely got NPM installed twice which will install the commands into 2 different folders:
Once into C:\Users\<user>\AppData\Roaming\npm and once into C:\dev\nodist\bin\bin.
C:\dev\nodist\bin\bin wasn't on my path variable so I added it, and I removed the Node.JS version I didn't want to use.
If you have no grunt.cmd file created by npm, make sure that you do not have a .npmrc in your home directory with: bin-links=false in it.
After getting a tonne of "'grunt' is not recognized as an internal or external command," errors, I solved this on Windows 10 by going to Path and adding C:\Users\Username\AppData\Roaming\npm

Resources