How to install npm -g on offline server
Asked Answered
R

11

31

I need to install a "global" npm applications on an offline server.

It is easy to install a normal application:

npm install

and then pack up the resulting files. Either manually or using npm pack.

However, how can I install global application (that has a install script of some sort) such as forever without Internet?

npm install -g forever
Rationality answered 2/7, 2012 at 13:50 Comment(0)
P
20

try npmbox, it is the new name of npmzip which will allow you to install offline npm packages by one file

Polemic answered 29/10, 2013 at 18:51 Comment(5)
I installed npmbox pn the server but how to install pm2 using that?Kramer
Sorry but didn't work for me. Spent quite a bit of time trying until desisted. Did find how and posted below.Caudate
npmbox is a dead project. Don't bother.Jehoash
my question how to install npmbox without internet access?Spellbound
npmbox does not work anymore.Meaty
A
19

You can install stuff from a tarball file, check out the npm documentation. You can find the URL of the forever tarball with npm view forever dist.tarball and download that. Try something like this:

curl -so forever.tar.gz `npm view forever dist.tarball 2> /dev/null`
npm install ./forever.tar.gz -g

But you might have to do this for all of the dependencies as well. There might be a better way but this is what I've found in my search.

Athapaskan answered 2/7, 2012 at 14:5 Comment(3)
Thanks for your answer. However, it immediately tries to pull in the dependencies from Internet. npm http GET registry.npmjs.org/broadwayRationality
Naturally, you would need to install any dependencies first.Blanchard
You are correct Kato. But how? There are a lot of dependencies for forever.Rationality
U
8

Well.... after a day trying to make it work with above references (npmbox or offline-npm) came up with something way much simpler. Thanks to npmbox I have to say. The idea is the keep the cache from the instance that has online access and then use it in the one offline.

In machine with internet:

1 - clear npm cache: npm cache clear

2 - install package, lets say its x.y.z: npm install -g **package.x.y.z**

3 - copy cache in to a folder... let's call it whatever (I assume npm cache is in root folder, not absolutely sure about that): cp -R /.npm/* **/cache-whatever-folder**

In machine with no internet:

4 - take this cache-whatever-folder to the instance with no internet and after that, clean cache and install with it (I won't indicate how to copy the folder :)

npm cache clear

npm install --global --cache **/cache-whatever-folder** --optional --cache-min 99999999999 --shrinkwrap false **package.x.y.z**

Done

Unitary answered 4/5, 2017 at 21:47 Comment(3)
npm-cache makes no guarantees that the cache will be complete or not corrupted. How this could happen could be due to auto cache cleanup, or hash collisions. The npm-cache docs say this "npm makes no guarantee that a previously-cached piece of data will be available later"Junno
do i just copy the npm-cache folder and replace in other machine? I am using windows not sure how u used the cp command. If you could elaborate the file path it would be awesome.Closeknit
Yes, @MurtazaHaji, get the data from %AppData%/npm-cache in the machine with internet access and copy it to the one the hasn't.Caudate
D
6

INSTALL PM2 OFFLINE:-

Tested on Node-v6.10.3 and Npm-3.10.10 on RHEL-7

Go to machine with internet connection:-

#npm install -g npmbox
#npmbox npmbox
#scp npmbox.npmbox root@offline-server-ip:.

Go to machine without internet connection :-

#ssh  root@offline-server-ip 
#tar --no-same-owner --no-same-permissions -xvzf npmbox.npmbox
#npm install --global --cache ./.npmbox.cache --optional --cache-min 99999999999 --shrinkwrap false npmbox

Go to machine with internet connection:-

#npm install pm2 -g
#npmbox pm2
#scp pm2.npmbox root@offline-server-ip:.

Go to machine without internet connection :-

#npmunbox pm2.npmbox --global
#pm2 ls
Diplomatic answered 17/7, 2017 at 12:7 Comment(0)
J
4

I created offline-npm for getting all the dependencies installed in a clean way. For modules without the use of node-gyp everything should work as described.

If you require node-gyp (which is usually installed online) consider copying ~/.node-gyp to that offline machine.

Jobbery answered 20/12, 2014 at 9:9 Comment(0)
G
3

Try npmzip

npm install -g npmzip
npmzip <package>

You will get the tarball in the current directory This may be moved to the target machine and :

npmunzip <tarball>
Glandulous answered 14/8, 2013 at 13:38 Comment(1)
npm can handel tar.gz zip git svn by default manualSeparate
M
3

Using Yarn:

  1. On the internet machine (configure local cache location):

    yarn config set yarn-offline-mirror ~/yarn-offline-mirror/
    
  2. On the offline machine (configure local cache location):

    yarn config set yarn-offline-mirror ~/yarn-offline-mirror/
    
  3. On the offline machine, Find out where is the global installation location:

    yarn global bin
    

    (Or set it with yarn config set prefix <file_path>)

  4. On the offline machine, add it to your path. E.g.:

    echo 'export PATH=$PATH:'"$(yarn global bin)" >> ~/.bashrc  
    source ~/.bashrc # reload
    
  5. On the internet machine, download forever's dependencies:

    mkdir new-cli-forever/
    cd new-cli-forever/
    yarn add forever
    

    Then copy new-cli-forever/yarn.lock and ~/yarn-offline-mirror/ to the offline machine. (rm -rf new-cli-forever/ is ok now.)

  6. On the offline machine, install forever from local cache:

    cp /path/to/imported/yarn.lock .
    cp -n /path/to/imported/yarn-offline-mirror/* ~/yarn-offline-mirror/
    yarn global add --offline forever
    rm -f ./yarn.lock
    

For more info, see my post here: https://assafmo.github.io/2018/04/11/yarn-offline.html

Mensal answered 14/4, 2018 at 13:28 Comment(4)
How do you get yarn to the offline machine in the first place?Sclater
Thanks a lot, this is the only solution of this thread that worked for me. My only remark is that in my case, I had to run yarn --offline to create the project before yarn global add --offline dep. Otherwise, it did not workAnorthic
Oh, and also, you can force engine versions with this option : --ignore-engines. For instance, yarn add --ignore-engines dep In my case, this came helpful to install pm2Anorthic
Umm. How to get yarn on the offline server?Cambell
P
2

List the dependencies in bundledDependencies in your package.json, and then run npm pack to create a tarball. Get that over to the other machine, and either npm install <tarball>, or just crack it open manually.

https://github.com/npm/npm/issues/1349

Philosophical answered 9/10, 2015 at 16:2 Comment(0)
T
2

npmbox is outdated

Use npm pack command (npm docs), no need to install anything.

Single package

If you want only one package (for example, forever), you can run:

npm pack forever

this command will fetch it to the cache, and then copy the tarball to the current working directory as -.tgz

Then, from the folder you created the package, you install it with:

npm install -g ./forever-x.y.z.tgz

Whole project

If you want a whole project to be installed offline, include a poperty in your package.json named bundleDependencies and list all dependecies you need in this field.

// package.json

"dependencies": {
  "archiver": "^2.1.1",
  "axios": "^0.16.2",
  "body-parser": "^1.18.3"
},
"bundleDependencies": {
  "archiver": "^2.1.1",
  "axios": "^0.16.2",
  "body-parser": "^1.18.3"
}

Then run npm pack.

It will create a .tgz file of your whole project and dependencias.

You just have to copy it to the offline server and untar.

Truism answered 5/7, 2019 at 6:35 Comment(0)
R
0

On your local machine or any machine that has internet connection, do

npm install npm-bundle -g
npm install forever -g

Now, go to cd /usr/local/lib/node_modules/forever and do

npm-bundle

It will create a .tgz file. Now scp/ftp that .tgz file to the offline server and do

npm install forever -g

Reference: This blog

Recife answered 27/5, 2019 at 9:13 Comment(1)
Followed all steps , still npm install timesout because its trying to connect to registry.npmjs.org which isnt allowed in my production machineCloseknit
P
0

On an online machine do:

# change to desired packages
packages="pm2 yarn"
for pkg in $packages; do
    mkdir "$pkg"
    curl "$(npm view "$pkg" dist.tarball)" |
    tar -xzC "$pkg" --strip-components 1
    (cd "$pkg" && npm install)
done
tar -czf packages.tar.gz $packages

On the offline server do:

npm config set offline
tar -xzf packages.tar.gz
npm install -g pm2 yarn
Pantheas answered 25/3, 2021 at 21:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.