Published on

Private NPM Server

Authors
  • avatar
    Name
    PatharaNor
    Twitter

local-npm-fetch-log

Preparing Environment

Local npm server

Our local npm server is in machine .240, we using verdaccio framework to provide software dependency packages to client.

Note

Now it's running with screen command.

avatar
PatharaNor
Tech Writer

Initial local npm server :

init-local-npm

Local npm log :

local-npm-fetch-log

Point to local npm server

In case you doesn't install nodejs yet, please enable proxy and install it first:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
sudo npm install -g n
sudo n 10.15 # or your version

After that you must close proxy.

# Unset proxy
unset http_proxy && unset https_proxy

Change npm registry to local-npm server

Our service using private network, we cannot connect to public network directly. So we will set npmserver to our local-npm server instead.

npm set registry http://172.18.62.240:5080

Now we can use command npm install to install software/library dependencies in our project.

Example pm2

IMPORTANT: in case the machine didn't provide pm2 command, we must install it first. (need to use sudo command)

# Using "sudo" to support "npm install -g ..."
sudo npm set registry http://172.18.62.240:5080
# Set npm prefix
npm get prefix
npm config set prefix /usr/local

# Install your dependencies(example "pm2")
sudo npm install pm2 –g

# Switch back to normal installation "npm install ..."
npm set registry http://172.18.62.240:5080

After that you should call /usr/local/bin/pm2 able.

[Optional] If it existing, update npm

sudo npm i -g npm

⚠️ Caution

In case you found FAIL linked path in /usr/local/bin directory, please remove it first:

sudo rm -rf /usr/local/bin/pm2*
avatar
PatharaNor
Tech Writer

IMPORTANT: IF YOUR BOTH COMMANDS ARE IN OUR TARGET DIRECTORY, PLEASE REJECT CONTENT BELOW.

Our target npm command directory is /usr/local/bin :

# sudo ln -s INSTALLED_DIRECTORY/pm2 /usr/local/bin/pm2 && \
# sudo ln -s INSTALLED_DIRECTORY/pm2-dev /usr/local/bin/pm2-dev && \
# sudo ln -s INSTALLED_DIRECTORY/pm2-docker /usr/local/bin/pm2-docker && \
# sudo ln -s INSTALLED_DIRECTORY/pm2-runtime /usr/local/bin/pm2-runtime
#
# INSTALLED_DIRECTORY can be
#  - /app/hms-gateway/node_modules/pm2/bin
#  - /usr/bin

# example:
sudo ln -s /usr/bin/pm2 /usr/local/bin/pm2 && \
sudo ln -s /usr/bin/pm2-dev /usr/local/bin/pm2-dev && \
sudo ln -s /usr/bin/pm2-docker /usr/local/bin/pm2-docker && \
sudo ln -s /usr/bin/pm2-runtime /usr/local/bin/pm2-runtime

Issue

  1. Permission denied on pm2 command

Start from clean up everything both npm and pm2. After that manually change npm’s default directory to resolving EACCES permissions errors when installing packages globally.

And then re-install pm2 again without sudo, please refer to chain command below:

sudo npm uninstall npm -g && \
npm config delete prefix && \
nvm install stable && \
nvm use stable && \
npm -v && \
npm i pm2 -g && \
mkdir ~/.npm-global && \
npm config set prefix '~/.npm-global' && \
export PATH=~/.npm-global/bin:$PATH && \
(sudo pm2 kill || :) && \
(npm remove pm2 -g || :) && \
(sudo rm -rf /bin/pm2* || :) && \
(sudo rm -rf /usr/local/bin/pm2* || :) && \
(sudo which pm2 || :) && \
NPM_CONFIG_PREFIX=~/.npm-global && \
npm install -g pm2 && \
~/.npm-global/bin/pm2

Finally create symlink to common pm2 path for Jenkins:

sudo ln -s ~/.npm-global/bin/pm2 /usr/local/bin/pm2 && \
sudo ln -s ~/.npm-global/bin/pm2-dev /usr/local/bin/pm2-dev && \
sudo ln -s ~/.npm-global/bin/pm2-docker /usr/local/bin/pm2-docker && \
sudo ln -s ~/.npm-global/bin/pm2-runtime /usr/local/bin/pm2-runtime

Testing by run command below:

/usr/local/bin/pm2

You should see contents like this:

usage: pm2 [options] <command>

pm2 -h, --help             all available commands and options
pm2 examples               display pm2 usage examples
pm2 <command> -h           help on a specific command

Access pm2 files in ~/.pm2

⚠️ Caution

Last version of pm2 of the installation is 4.4.0 but the old version(on-premise) is 2.10.4.

avatar
PatharaNor
Tech Writer