2

I have frequently ran into the issue of not being able to use npm or node <filename> within vscodes terminal. I have scoured the internet for the answer yet none of them have worked for me. I have done the following which have been suggested by others. All have been done on a windows machine.

Note: when running npm i and/or npm init I do not get the same errors

  1. uninstall/reinstall node program from machine(windows 10)
  2. delete any and all traces of node from programs, folders and pathing's
  3. add environment variable path
  4. shutdown and restart terminals
  5. using gitbash terminal as well as the command prompt to access get the command to work
  6. uninstalling git after the new node has been installed

Example

npm ERR! code 1
npm ERR! path C:\Users\bai1e\letstudy\node_modules\protobufjs 
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c C:\Users\bai1e\AppData\Local\Temp\postinstall-a42f6975.cmd
npm ERR! 'node' is not recognized as an internal or external command,
npm ERR! operable program or batch file.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\bai1e\AppData\Local\npm-cache\_logs\2022-09-15T02_47_40_653Z-debug-0.log

Portion of Log

0 verbose cli C:\Program Files\nodejs\node.exe C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js
1 info using [email protected]
2 info using [email protected]
3 timing npm:load:whichnode Completed in 0ms
4 timing config:load:defaults Completed in 4ms
5 timing config:load:file:C:\Program Files\nodejs\node_modules\npm\npmrc Completed in 2ms
6 timing config:load:builtin Completed in 2ms
7 timing config:load:cli Completed in 1ms
8 timing config:load:env Completed in 1ms
9 timing config:load:file:C:\Users\bai1e\letstudy\.npmrc Completed in 0ms
10 timing config:load:project Completed in 2ms
11 timing config:load:file:C:\Users\bai1e\.npmrc Completed in 0ms
3
  • Can you share the log? What does that say? Commented Sep 15, 2022 at 5:20
  • Maybe your PATH variable is messed up in a subtle way which affects different software differetnly. Take a look at this answer to a very similar issue. If that's not it, it'd be important to know what PATH variable the inner cmd.exe process sees which is called by npm. You can get the software Process Monitor and configure the filter to Operation begins with Process and then capture a trace while the issue occurs. Save the trace to a file and share it in your question Commented Aug 29, 2023 at 7:55
  • There is another possibility here, as I described in this answer - if your PATH has a file instead of a folder somewhere in the middle of a path (like C:\Windows\explorer.exe\hello), then Git Bash will truncate the PATH when passing it into node.exe, and then cmd.exe called from node.exe won't find node itself anymore. (Again, this would be revealed by looking at the Process Monitor trace.) Commented Aug 29, 2023 at 8:04

1 Answer 1

-4

If you have already installed Node.js on your machine and added it to your environment variables, but you are still getting the "node is not recognized" error, it could be that Visual Studio Code is not correctly recognizing the Node.js installation.

To fix this, you can try the following steps:

  1. Open Visual Studio Code and go to File > Preferences > Settings.
  2. In the settings, search for "terminal.integrated.inheritEnv" and check the box to enable it. This will make Visual Studio Code inherit the environment variables from your system.
  3. If the above step doesn't work, you can try setting the Node.js path explicitly in the Visual Studio Code settings. Add the following line to your user or workspace settings file:

"terminal.integrated.shellArgs.windows": ["/k", "C:\Program Files\nodejs\nodevars.bat"]

Make sure to replace the path "C:\Program Files\nodejs\nodevars.bat" with the actual path to your Node.js installation.

  1. Restart Visual Studio Code and try running the npm or node command in the terminal again.

If the above steps don't work, you can also try reinstalling Visual Studio Code and making sure to install the "Node.js Extension Pack" extension, which provides better support for Node.js development within Visual Studio Code.

Note: If you installed Node.js after opening Visual Studio Code, you may need to restart Visual Studio Code for it to recognize the Node.js installation.

Sign up to request clarification or add additional context in comments.

3 Comments

As you can see from the log, npm (and therefore node) is already running, but an inside call to node from an npm script fails. So it can't be VSCode.
did any of this content come from chatgpt or other AI?
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.