Home > Blockchain >  NPM packages on WSL2 that require script to run have permission denied
NPM packages on WSL2 that require script to run have permission denied

Time:07-26

My wsl2 (ubuntu 20.04) is set to use root user.

I can install any node package that does not require a script to run. If it does require a script like the node-sass package then I get the error:

sh: 1: node: Permission denied

Same happens for npx commands like:

# npx npkill
/tmp/npx-a435a586.sh: 1: npkill: Permission denied

There are many threads here that state the fix is to set these values:

npm config set user 0
npm config set unsafe-perm true

But this does not change the behavior and the same error occurs. I have tried deleting node modules and also using the --unsafe-perm flag but it still has same error. I have also tried restarting after changing node settings.

My versions:

  • node: v16.13.2
  • npm: 8.15.0
  • wsl: Ubuntu-20.04

Anything else I can try?

CodePudding user response:

I know that it doesn't seem helpful for me to say that it's working for me, but hopefully by walking through what I've done and comparing it to your installation we can spot a difference that might be causing the problem for you.

On a fresh installation of Ubuntu 20.04 (distribution named "Ubuntu") on WSL2:

sudo apt update && sudo apt upgrade -y

# Since you mention that you have set your user to be root:
sudo sh -c 'echo "[user]\ndefault=root" > /etc/wsl.conf'

wsl.exe -l -v
# Confirm your distribution name for the following command:
wsl.exe --terminate Ubuntu

Restart WSL, the user is now root:

# Use n version manager -- Just my personal preference:
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
bash n 16.13.2
npm install -g [email protected]

npm install -g npkill
npx npkill

And it works as expected.


If you'd like to test it out on a "clean" distribution (without disturbing your existing one, that you can then compare), you can do the following. First, in an Administrative PowerShell:

Get-ChildItem -Recurse 'C:\Program Files\WindowsApps\*' |
Where-Object {$_.Name -eq 'install.tar.gz' } |
% { $_.FullName }

Copy the path for the distribution the clipboard for use later. It should be something like:

C:\Program Files\WindowsApps\CanonicalGroupLimited.UbuntuonWindows_2004.2022.1.0_x64__79rhkp1fndgsc\install.tar.gz

Back in a "Normal user" PowerShell session:

# Adjust paths/names as desired
mkdir $env:userprofile/WSL/instances/TestUbuntu
wsl --import TestUbuntu $env:userprofile/WSL/instances/TestUbuntu "<path_copied_above_in_quotes>" --version 2
wsl ~ -d TestUbuntu
  • Related