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
- uninstall/reinstall node program from machine(windows 10)
- delete any and all traces of node from programs, folders and pathing's
- add environment variable path
- shutdown and restart terminals
- using gitbash terminal as well as the command prompt to access get the command to work
- 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
PATHvariable 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 whatPATHvariable the innercmd.exeprocess sees which is called by npm. You can get the software Process Monitor and configure the filter toOperationbegins withProcessand then capture a trace while the issue occurs. Save the trace to a file and share it in your questionPATHhas a file instead of a folder somewhere in the middle of a path (likeC:\Windows\explorer.exe\hello), then Git Bash will truncate thePATHwhen 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.)