Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 7091

Windows Subsystem for Linux (WSL) using shared Node.js installation with Windows: Node.js npm & npx binaries not working

$
0
0

I have recently moved to a Windows + WSL environment (WSL is going very good by the way). The main reason of doing this is to have a Linux environment for development and having Windows for other apps & games without having to reboot my computer (had a dual-boot setup before).

In the setup process, I found most of the Windows installed binaries can be executed from WSL. So instead of duplicating installations (eg: installing java and maven in Windows in order to use Eclipse IDE and then installing it in WSL separately to use it in the terminal) I could just install java jdk in Windows and symlink the binaries to WSL in order to share the jdk installation, this worked flawlessly). But doing the same with node, happens that node npm and npx binaries are not working :(

I wannted to have a single node installation which I could manage using using nvm windows. So I started the installation the following way:

In WSL, I configured my /etc/wsl.conf, following Nick Janetakis guide here (thanks Nick) in order to mount Windows drives at / instead of /mnt/:

/etc/wsl.conf

[automount]root = /options = "metadata"

Then installed node in windows:

C:\Windows\system32> nvm install 10.15.0... installing process...C:\Windows\system32> nvm use 10.15.0...success message...C:\Windows\system32> node -vv10.15.0C:\Windows\system32> npm -v6.4.1

Everything working as expected so far. The next step is to symlink the windows node binaries to WSL. The binaries are located at:

C:\Windows\system32> where nodeC:\Program Files\nodejs\node.exeC:\Windows\system32> where npmC:\Program Files\nodejs\npmC:\Program Files\nodejs\npm.cmdC:\Windows\system32>where npxC:\Program Files\nodejs\npxC:\Program Files\nodejs\npx.cmd

So inside WSL terminal (remember that my disks are mounted at /c not /mnt/c as the default behaviour):

user@host:~$ mkdir ~/binuser@host:~$ ln -s /c/Program\ Files/nodejs/node.exe ~/bin/nodeuser@host:~$ ln -s /c/Program\ Files/nodejs/npm ~/bin/npmuser@host:~$ ln -s /c/Program\ Files/nodejs/npx ~/bin/npx

And...

user@host:/d/tmp$ node -vv10.15.0user@host:/d/tmp$ echo "console.log('Hello World');">> index.jsuser@host:/d/tmp$ node index.jsHello World

Great! (Note: as node is installed on windows, when being on WSL you must use it inside a disk drive, /d in this case). But...

user@host:~$ npm -vinternal/modules/cjs/loader.js:583throw err;^Error: Cannot find module 'C:\home\user\bin\node_modules\npm\bin\npm-cli.js'at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)at Function.Module._load (internal/modules/cjs/loader.js:507:25)at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)at startup (internal/bootstrap/node.js:283:19)at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

Now that's the reason I'm writing this. The error is clear, npm is trying to find npm-cli.js in a path which is a wired mix of the npm symlink location inside a windows path.

Is there a way to tell npm/npx the correct Windows path where it must find its files from WSL?

Sorry for the long question but due to the very particular setup I considered that contextualization necessary.


Viewing all articles
Browse latest Browse all 7091

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>