Home > Web Front-end > JS Tutorial > The principle and solution of invalid node.js version management tool n

The principle and solution of invalid node.js version management tool n

高洛峰
Release: 2016-12-05 14:18:52
Original
1357 people have browsed it

Introduction

n is a module of node, which can be used to manage various versions of node. Similar to pyenv in Python and rbenv in Ruby. The author of n is the famous TJ master.

Install n via npm:

$ npm install -g n
Copy after login

View the current node version:

$ node -v
v4.2.4
Copy after login

Install the specified version via n:

$ n 4.4.4
install : node-v4.4.4
    mkdir : /opt/node/n/versions/node/4.4.4
    fetch : https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.gz
###################################100.0%
  installed : v4.2.4
Copy after login

Check the current version again node version:

$ node -v
v4.2.4 #和原来一样
Copy after login

Solution

If you are like me and find that there is no change in the node version, then the most likely situation is that your node installation directory is different from the default path.

Check the current installation path of node:

$ which node
/opt/node/bin/node #举个例子
Copy after login

The default installation path is /usr/local. If your node is not in this path, you cannot copy bin, lib, include, and share when switching versions. in this path, so we must modify the default node installation path of n through the N_PREFIX variable.

Edit the environment configuration file:

vim ~/.bash_profile
Copy after login

Insert the following two lines of code at the end of the file:

export N_PREFIX=/opt/node #node实际安装位置
export PATH=$N_PREFIX/bin:$PATH
Copy after login

:wq save and exit;

Execute source to make the modifications effective.

$ source ~/.bash_profile
Copy after login

Confirm whether the environment variable is effective:

echo $N_PREFIX
/opt/node
Copy after login

At this time we need to reinstall:

$ n 4.4.4
install : node-v4.4.4
    mkdir : /opt/node/n/versions/node/4.4.4
    fetch : https://nodejs.org/dist/v4.4.4/node-v4.4.4-linux-x64.tar.gz
##############100.0%
  installed : v4.4.4
Copy after login

Check the current node version again:

$ node -v
v4.4.4
Copy after login

Description modification success.


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template