Node.js tips and tricks

From HPCWIKI
Jump to navigation Jump to search

Update nodejs package.json

# Install npm-check-updates globally
$ npm i -g npm-check-updates

# Inside of nodejs project folder where package.json is located
$ ncu -u
Upgrading /opt/imp/home/prj/rim/package.json
[====================] 39/39 100%

 async                           ^2.6.3  →    ^3.2.6
 canvas-gauges                   ^2.1.5  →    ^2.1.7
 cheerio                    ^1.0.0-rc.2  →    ^1.0.0
 cookie-parser                   ^1.4.4  →    ^1.4.7
....

# Run npm install to install new versions.
$ npm install

Install NVM

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
source ~/.bashrc

# List up node version
nvm list-remote

# Install specific version
nvm install 18.0.0

# List of installed versions
nvm ls

# Change version
nvm use 16.15.0

# Update npm 
npm install -g npm@latest

Install latest Node.js[1]

#Install Node.js 20.x LTS
$curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash - 
$sudo apt-get install -y nodejs

Change node version

# Get available version list using nvm
$ nvm ls
         v4.9.1
        v6.17.1
         v8.8.1
         v8.9.4
        v8.17.0
       v10.24.1
->     v14.15.1
       v16.20.0
       v18.16.0
         system
default -> v14.15.1

# change version v18 temporary and check the changing version
$ nvm use v18.16.0
$ nvm ls
         v4.9.1
        v6.17.1
         v8.8.1
         v8.9.4
        v8.17.0
       v10.24.1
       v14.15.1
       v16.20.0
->     v18.16.0
         system
default -> v14.15.1             << default version will not be changed

# change default version v18 sytemwide
$ nvm alias default v18
default -> v18 (-> v18.16.0)

$ nvm ls
         v4.9.1
        v6.17.1
         v8.8.1
         v8.9.4
        v8.17.0
       v10.24.1
       v14.15.1
       v16.20.0
->     v18.16.0
         system
default -> v18 (-> v18.16.0)    << default version has been changed
node -> stable (-> v18.16.0) (default)
stable -> 18.16 (-> v18.16.0) (default)

# Install new version of node
$ nvm install <version you want to>

References