Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

npm doesn't work in PowerShell, only npm.cmd does #4436

Open
2 tasks done
OksanaMala opened this issue Jul 4, 2024 · 26 comments
Open
2 tasks done

npm doesn't work in PowerShell, only npm.cmd does #4436

OksanaMala opened this issue Jul 4, 2024 · 26 comments

Comments

@OksanaMala
Copy link

Node.js Version

v20.15.0

NPM Version

v10.8.0

Operating System

Windows 11 Pro

Subsystem

console, Other

Description

In PowerShell terminal, the npm command doesn't work; only npm.cmd works. However, in Command Prompt, the npm command works. To troubleshoot, I have reinstalled Node.js, PowerShell, checked environment variables, confirmed the path is set correctly, and npm.cmd exists in the nodejs folder.

Minimal Reproduction

No response

Output

No response

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
@RedYetiDev
Copy link
Member

@anonrig Is this nodejs/node#53538?

@hzhang20902
Copy link

hzhang20902 commented Jul 5, 2024

Can you confirm you installed node.js in the powershell environment, and not just cmd prompt (node -v)? Yes I know its weird, but sometimes it just happens. If you have, try running as admin and:

npm install -g npm

with npm.cmd for the first npm.

If that still doesn't work, you will have to go into your PATH environment and manually set 'npm' as a command variable bc if npm.cmd works then you definitely have npm, but for whatever reason the PATH variable never got set or never got set globally, or was removed.

@OksanaMala
Copy link
Author

Thanks for the advice and links, so far manipulating the environment variable has not helped. I will try the advice provided in the links. At the same time, when selecting сmd or git bash, the command npm works in the terminal, the problem is only with the PowerShall

@RedYetiDev
Copy link
Member

Thanks for the info! Could you glance over the linked issue and let me know whether they are the same problem? Thanks!

@RedYetiDev
Copy link
Member

RedYetiDev commented Jul 7, 2024

Hey @OksanaML78, did you get a chance to test the comment above? If so, how did it go? (What happens with npx commands?)

Also, did @hzhang20902 npm install -g npm work?

@OksanaMala
Copy link
Author

Everything works fine for me on the command line, why it doesn’t work with PowerShall I haven’t found a solution. I just started working through the command line

@OksanaMala
Copy link
Author

after npm install -g npm nothing changed in PowerShall. I used this command before but there are no changes

@RedYetiDev
Copy link
Member

RedYetiDev commented Jul 8, 2024

Hmm, strange, what about reinstalling Node.js from the official website?

@OksanaMala
Copy link
Author

I reinstalled not only the Node.js but also the PowerShall several times from the official website - no changes.....I wrote about it

@RedYetiDev
Copy link
Member

Oh, sorry, I didn't refresh myself on the issue contents, my bad.

@nodejs/npm PTAL

@OksanaMala
Copy link
Author

OksanaMala commented Jul 8, 2024

I checked all the folders indicated in the links, I installed the installation from the same account from which I am executing the commands - but it works in PowerShall only this way npm.cmd , again with the command line and gitBash there are no problems - command npm work

@hzhang20902
Copy link

I checked all the folders indicated in the links, I installed the installation from the same account from which I am executing the commands - but it works in PowerShall only this way npm.cmd , again with the command line and gitBash there are no problems - command npm work

it would help to put in the error msg, or some other context, bc we're only guessing based off little bits of info you're giving us here and there.

did you explicitly set the PATH env variable to be executed when you run the 'npm' command within powershell, to be "C:\Program Files\nodejs", or a prefix file that describes similar actions? As in, you can confirm that you yourself did this, or have found the prefix file specifically for Powershell that states NODE EXE and NPM PREFIX JS, or NPM CLI JS, or some other combination of those variable names are set to some kind of nodejs directory, npm prefix file, something?

you should be able to open the files in the nodejs directory to check, if you have vs code, use code "C:\Program Files\nodejs" if not use Notepad command instead of code command. any shell environment will work

Because if npm.cmd works, then it's just something not set correctly with your Path to the executable. The nodejs directory in Program Files includes npm command prefixes for all 3 of the shells you mentioned, cmd prompt, bash, and powershell, in 3 separate files, as well as versions for the npx command.

which means whenever you installl node, it automatically comes with scripts that sets it for those 3 shell environments. it's all or nothing.

but again, not much context to go off of. however, you should look into if these are set correctly first before anything else, bc that seems to be the root of your problem.

@OksanaMala
Copy link
Author

OksanaMala commented Jul 9, 2024

Yes of course, this is what I have and the errors, I can show -> cmd: C:\Windows\System32>npm audit
npm error code ENOLOCK
npm error audit This command requires an existing lockfile.
npm error audit Try creating one first with: npm i --package-lock-only
npm error audit Original error: loadVirtual requires existing shrinkwrap file
npm error A complete log of this run can be found in: C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-debug-0.log

in this log file 2024-07-09T08_45_08_197Z-debug-0.log ->

0 verbose cli C:\Program Files\nodejs\node.exe C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js
1 info using npm@10.8.1
2 info using node@v20.15.0
3 silly config load:file:C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\npmrc
4 silly config load:file:C:\Windows\System32.npmrc
5 silly config load:file:C:\Users\oksa.npmrc
6 silly config load:file:C:\Users\oksa\AppData\Roaming\npm\etc\npmrc
7 verbose title npm audit
8 verbose argv "audit"
9 verbose logfile logs-max:10 dir:C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-
10 verbose logfile C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-debug-0.log
11 silly packumentCache heap:4345298944 maxSize:1086324736 maxEntrySize:543162368
12 silly logfile start cleaning logs, removing 1 files
13 verbose stack Error: loadVirtual requires existing shrinkwrap file
13 verbose stack at Arborist.loadVirtual (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\node_modules@npmcli\arborist\lib\arborist\load-virtual.js:51:18)
13 verbose stack at async Arborist.audit (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\node_modules@npmcli\arborist\lib\arborist\index.js:241:14)
13 verbose stack at async Audit.auditAdvisories (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\commands\audit.js:69:5)
13 verbose stack at async Audit.exec (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\commands\audit.js:49:7)
13 verbose stack at async Npm.exec (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\npm.js:207:9)
13 verbose stack at async module.exports (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\cli\entry.js:74:5)
14 error code ENOLOCK
15 error audit This command requires an existing lockfile.
16 error audit Try creating one first with: npm i --package-lock-only
17 error audit Original error: loadVirtual requires existing shrinkwrap file
18 silly unfinished npm timer audit 1720514708724
19 verbose cwd C:\Windows\System32
20 verbose os Windows_NT 10.0.22631
21 verbose node v20.15.0
22 verbose npm v10.8.1
23 verbose exit 1
24 verbose code 1
25 error A complete log of this run can be found in: C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-debug-0.log

environment variable in cmd->

C:\Windows\System32>echo %PATH%
C:\Python312\Scripts;C:\Python312;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Program Files\dotnet;C:\Program Files\Microsoft SQL Server\150\Tools\Binn;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn;C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn;C:\Program Files\Azure Data Studio\bin;C:\Program Files (x86)\Microsoft SQL Server\160\Tools\Binn;C:\Program Files\Microsoft SQL Server\160\Tools\Binn;C:\Program Files\Microsoft SQL Server\160\DTS\Binn;C:\ProgramData\chocolatey\bin;C:\Users\oksa\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\PowerShell\7;C:\Program Files\nodejs;C:\Users\oksa\AppData\Local\Microsoft\WindowsApps;C:\Users\oksa.dotnet\tools;C:\Users\oksa\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\oksa\AppData\Roaming\npm

@OksanaMala
Copy link
Author

OksanaMala commented Jul 9, 2024

in PowerShall: Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Windows\system32> npm -v
PS C:\Windows\system32> npm.cmd -v
10.8.1
PS C:\Windows\system32>
as you cam see command npm -v did nothing and when this command is executed, a window for selecting a program opens

@hzhang20902
Copy link

Yes of course, this is what I have and the errors, I can show -> cmd: C:\Windows\System32>npm audit npm error code ENOLOCK npm error audit This command requires an existing lockfile. npm error audit Try creating one first with: npm i --package-lock-only npm error audit Original error: loadVirtual requires existing shrinkwrap file npm error A complete log of this run can be found in: C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-debug-0.log

in this log file 2024-07-09T08_45_08_197Z-debug-0.log ->

0 verbose cli C:\Program Files\nodejs\node.exe C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\bin\npm-cli.js 1 info using npm@10.8.1 2 info using node@v20.15.0 3 silly config load:file:C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\npmrc 4 silly config load:file:C:\Windows\System32.npmrc 5 silly config load:file:C:\Users\oksa.npmrc 6 silly config load:file:C:\Users\oksa\AppData\Roaming\npm\etc\npmrc 7 verbose title npm audit 8 verbose argv "audit" 9 verbose logfile logs-max:10 dir:C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z- 10 verbose logfile C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-debug-0.log 11 silly packumentCache heap:4345298944 maxSize:1086324736 maxEntrySize:543162368 12 silly logfile start cleaning logs, removing 1 files 13 verbose stack Error: loadVirtual requires existing shrinkwrap file 13 verbose stack at Arborist.loadVirtual (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\node_modules@npmcli\arborist\lib\arborist\load-virtual.js:51:18) 13 verbose stack at async Arborist.audit (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\node_modules@npmcli\arborist\lib\arborist\index.js:241:14) 13 verbose stack at async Audit.auditAdvisories (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\commands\audit.js:69:5) 13 verbose stack at async Audit.exec (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\commands\audit.js:49:7) 13 verbose stack at async Npm.exec (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\npm.js:207:9) 13 verbose stack at async module.exports (C:\Users\oksa\AppData\Roaming\npm\node_modules\npm\lib\cli\entry.js:74:5) 14 error code ENOLOCK 15 error audit This command requires an existing lockfile. 16 error audit Try creating one first with: npm i --package-lock-only 17 error audit Original error: loadVirtual requires existing shrinkwrap file 18 silly unfinished npm timer audit 1720514708724 19 verbose cwd C:\Windows\System32 20 verbose os Windows_NT 10.0.22631 21 verbose node v20.15.0 22 verbose npm v10.8.1 23 verbose exit 1 24 verbose code 1 25 error A complete log of this run can be found in: C:\Users\oksa\AppData\Local\npm-cache_logs\2024-07-09T08_45_08_197Z-debug-0.log

environment variable in cmd->

C:\Windows\System32>echo %PATH% C:\Python312\Scripts;C:\Python312;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\Git\cmd;C:\Program Files\dotnet;C:\Program Files\Microsoft SQL Server\150\Tools\Binn;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn;C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn;C:\Program Files\Azure Data Studio\bin;C:\Program Files (x86)\Microsoft SQL Server\160\Tools\Binn;C:\Program Files\Microsoft SQL Server\160\Tools\Binn;C:\Program Files\Microsoft SQL Server\160\DTS\Binn;C:\ProgramData\chocolatey\bin;C:\Users\oksa\AppData\Roaming\nvm;C:\Program Files\nodejs;C:\Program Files\PowerShell\7;C:\Program Files\nodejs;C:\Users\oksa\AppData\Local\Microsoft\WindowsApps;C:\Users\oksa.dotnet\tools;C:\Users\oksa\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\oksa\AppData\Roaming\npm

for the last command, you can use for %a in ("%path:;=";"%") do @echo %~a so that it prints it out line by line and is easier to read. just FYI, in case you need later. I will reply to your next comment as it is much shorter so you can read response better

@OksanaMala
Copy link
Author

PowerShall ->

PS C:\Windows\system32> $env:Path -split ";"
C:\Python312\Scripts
C:\Python312
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0
C:\Windows\System32\OpenSSH
C:\Program Files\Git\cmd
C:\Program Files\dotnet
C:\Program Files\Microsoft SQL Server\150\Tools\Binn
C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn
C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn
C:\Program Files\Azure Data Studio\bin
C:\Program Files (x86)\Microsoft SQL Server\160\Tools\Binn
C:\Program Files\Microsoft SQL Server\160\Tools\Binn
C:\Program Files\Microsoft SQL Server\160\DTS\Binn
C:\ProgramData\chocolatey\bin
C:\Users\oksa\AppData\Roaming\nvm
C:\Program Files\nodejs
C:\Program Files\PowerShell\7
C:\Program Files\nodejs
C:\Users\oksa\AppData\Local\Microsoft\WindowsApps
C:\Users\oksa.dotnet\tools
C:\Users\oksa\AppData\Local\Programs\Microsoft VS Code\bin
C:\Users\oksa\AppData\Roaming\npm
C:\Program Files\nodejs
PS C:\Windows\system32>

@hzhang20902
Copy link

in PowerShall: Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.

Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows

PS C:\Windows\system32> npm -v PS C:\Windows\system32> npm.cmd -v 10.8.1 PS C:\Windows\system32> as you cam see command npm -v did nothing and when this command is executed, a window for selecting a program opens

ok so this clears some things up. please in the future, when you have an error like this, explain the error in context, because no one can tell that the "error" you are receiving in powershell is not an error but just no output, because that means the process actually did run, and ran successfully, it just did not give you output in the powershell environment. remember that errors will ALWAYS be pointed out, and will show line by line, like that npm audit error log, so NO OUTPUT does NOT equal ERROR.

for reference, the shell is a REPL (read-evaluate-print loop) so it reads your input, evaluates which process to execute, and prints the result or output, in a loop. Like the error log is a line by line of this loop. The PRINT part means it will always OUTPUT something to somewhere.

Here, your issue is the SOMETHING is not going to the SOMEWHERE that you want it to.

There is indeed a SOMETHING, we know this because an error did not get raised (and you verified the nodejs installation and path env vars, etc). We just don't know the SOMEWHERE it got sent to.

This is probably caused by an AutoRun configuration being run either in your user account or global system. To fix it, try these first:

  • in Powershell run npm -v /d
  • if you see the version info, then there's an AutoRun Path being run before the command.
  • in PS, run Get-Command npm. this should show the ps1 shell script location '...nodejs\npm.ps1` and anything run before it. there should be nothing before it.
  • in command prompt, run where npm this will do the same thing, and should list '...nodejs\npm' and '\npm.cmd'
  • again, also nothing before it.
  • if there is anything else in either of the lists of paths shown, it's probably an AutoRun path, it will be one or both of these:
    • HKEY_LOCAL_MACHINE\Software....\AutoRun
    • HKEY_CURRENT_USER\Software....\AutoRun
  • the first is the global setting, the second is for the user profile
  • if either exists you need to disable them through this process
  • If that does not work, and running the npm -v /d did give you the info version then the only suggestion I have left is:
  • Completely uninstall and remove the traces of nodejs like those 3 paths, and I mean manually delete them if they are still there in Path after you uninstall, and then reinstall node.js NOT using Chocolatey. Use the direct executable that you can download from the Node.js website

I would suggest use the latest version of Node.js and NPM, which should be 22 something and 10.8 something. They should come that way if you choose the latest version in the link above, because If none of this works, then there is something within the npm.cmd and/or npm.ps1 files within the Program Files/nodejs/ directory that has been corrupted, or is being set incorrectly, for reasons beyond the scope of either node.js or any installations, and exceeds the capabilities of most users, like you and me, to be able to fix.

@RedYetiDev
Copy link
Member

Thanks for the detailed answer, @hzhang20902!

@OksanaML78 - if this resolves your issue, feel free to close this issue.

@OksanaMala
Copy link
Author

Thank you, I checked the registry again, because... I also checked earlier and I don’t have AutoRun in both folders, but nevertheless I once again deleted everything related to nodejs and installed the latest versions, but still the problem remained.
PS C:\Windows\system32> node -v
v22.4.1
PS C:\Windows\system32> npm -v
PS C:\Windows\system32> npm.cmd -v
10.8.1

@RedYetiDev RedYetiDev reopened this Jul 9, 2024
@RedYetiDev
Copy link
Member

If the problem persists, no need to close the issue

@OksanaMala
Copy link
Author

Thank you, but I can’t bother you anymore because... I already use the command line with which I have no problems, the problem with PowerShell is not so critical for me now

@RedYetiDev
Copy link
Member

It's no bother! If you leave this issue open, more users will see it, and they may have solutions.

@hzhang20902
Copy link

Thank you, I checked the registry again, because... I also checked earlier and I don’t have AutoRun in both folders, but nevertheless I once again deleted everything related to nodejs and installed the latest versions, but still the problem remained.
PS C:\Windows\system32> node -v
v22.4.1
PS C:\Windows\system32> npm -v
PS C:\Windows\system32> npm.cmd -v
10.8.1

Dang, thats really strange. Idk anymore, my best guess is audit your registry for any delayed expansion variables, but thats just based off the description of what they are, im not familiar with them. Weird that # running the cmd prompt command in PS outputs to PS but not its alias. You could try auditing the cmd extension as well, but at this point probably more trouble than its worth 😅

@YoussofKhawaja
Copy link

Just run this command in powershell as admin
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

What it does is it allows PowerShell scripts that are created on your local machine to run without needing a digital signature.

@coordova
Copy link

coordova commented Oct 4, 2024

I had the same issue...

> npm -v
npm : No se puede cargar el archivo C:\Program Files\nodejs\npm.ps1 porque la ejecución de scripts está
deshabilitada en este sistema. Para obtener más información, consulta el tema about_Execution_Policies en
https:/go.microsoft.com/fwlink/?LinkID=135170.
En línea: 1 Carácter: 1
npm -v

    CategoryInfo          : SecurityError: (:) [], PSSecurityException
    FullyQualifiedErrorId : UnauthorizedAccess

I ran this command in powershell as admin:

> Get-executionPolicy
Restricted
> Set-ExecutionPolicy Unrestricted
> npm -v
10.5.1

@lucasdoloh0v
Copy link

this work for me

I ran this command in powershell as admin:

> Get-executionPolicy
Restricted
> Set-ExecutionPolicy Unrestricted
> npm -v
10.5.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants