Problems and solutions for installing node in WSL

foreword

After downloading WSL+cmder, I don't want to open the Windows command line anymore, so I want to set a copy of the environment variables under Windows in WSL, Node is one of them. Since Windows binaries are incompatible with Linux, environment variables under Windows cannot be used directly. Therefore, you need to manually download node in linux yourself.

download

Under linux, there are many ways to download software. Since I don't like to use it apt-getfor downloading, I am going to download it myself.

First, find the link to the compiled linux download and installation package on the nodejs official website (do not select the source code download here, too lazy to compile).

Download the installation package: wget https://nodejs.org/dist/v10.15.1/node-v10.15.1-linux-x64.tar.xz.

Unzip the installation package:tar xvf node-v10.15.1-linux-x64.tar.xz

Move the decompressed files to the /usr/appdirectory, which is my custom software download directory.

Configure environment variables (the first)

Add nodejs to environment variables

Modify the configuration global environment variable configuration file: vim /etc/profile(requires root privileges)

Add the following:

export NODE_HOME=/usr/app/node-v10.15.1-linux-x64
export PATH=$PATH:$NODE_HOME/bin

The general idea is to add the directory of the node executable file (ie, the node environment variable) after all the environment variables.

Use a configuration file to take effect: source /etc/profileor重启

Configure configuration environment variables (second type)

Soft link the executable file to a /usr/bin/directory that contains all executable commands.

ln -s /usr/app/node-v10.15.1-linux-x64/bin/node /usr/bin/
ln -s /usr/app/node-v10.15.1-linux-x64/bin/npm /usr/bin/

Note: Soft links must use absolute paths .

question

node -vCheck it out

npm -vCheck for errors. The reason for the error: It finds the npm executable file in the node environment variable under Windows, and an error occurs.

reason

echo $PATHLooking at the environment variables, I found that there are two nodejs environment variable paths, the former is for windows, and the latter is for linux. The disadvantage of WSL is that the environment variables are mixed.

It seems to have found the problem: since the node environment variable of Windows takes precedence over that of linux, the system first matches the node environment variable of Windows. But node -vwhy can it be enforced? I don't know how to know (referred to as ignorance).

solve

Modify the /etc/profilefile

will be export PATH=$PATH:$NODE_HOME/binreplacedexport PATH=$NODE_HOME/bin:$PATH

That is to put the node environment variable before all environment variables. Don't forget to edit it source /etc/profile.


 

Guess you like

Origin blog.csdn.net/weixin_39842528/article/details/123476743