node.js 如何在离线服务器上安装 npm -g
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11295050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install npm -g on offline server
提问by Peter B
I need to install a "global" npm applications on an offline server.
我需要在离线服务器上安装“全局”npm 应用程序。
It is easy to install a normal application:
安装普通应用程序很容易:
npm install
and then pack up the resulting files. Either manually or using npm pack.
然后打包生成的文件。手动或使用npm pack。
However, how can I install global application (that has a install script of some sort) such as foreverwithout Internet?
但是,如何在没有 Internet 的情况下安装全局应用程序(具有某种安装脚本),例如永远?
npm install -g forever
采纳答案by kamil
回答by CodeRarity
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.tarballand download that. Try something like this:
您可以从 tarball 文件安装内容,请查看npm 文档。您可以找到永久 tarball 的 URLnpm view forever dist.tarball并下载它。尝试这样的事情:
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.
但是您可能还必须对所有依赖项执行此操作。可能有更好的方法,但这是我在搜索中发现的。
回答by Francisco López-Sancho
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.
嗯...。经过一天的尝试使其与上述参考(npmbox 或 offline-npm)一起工作后,想出了更简单的方法。感谢 npmbox 我不得不说。这个想法是从具有在线访问权限的实例中保留缓存,然后在离线时使用它。
In machine with internet:
在有互联网的机器上:
1 - clear npm cache: npm cache clear
1 - 清除 npm 缓存: npm cache clear
2 - install package, lets say its x.y.z: npm install -g **package.x.y.z**
2 - 安装包,让我们说它的 xyz: 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**
3 - 将缓存复制到一个文件夹中...让我们随便称呼它(我假设 npm 缓存在根文件夹中,对此不确定):
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 :)
4 - 将此缓存任何文件夹带到没有互联网的实例,然后,清理缓存并安装它(我不会说明如何复制文件夹:)
npm cache clear
npm cache clear
npm install --global --cache **/cache-whatever-folder** --optional --cache-min 99999999999 --shrinkwrap false **package.x.y.z**
npm install --global --cache **/cache-whatever-folder** --optional --cache-min 99999999999 --shrinkwrap false **package.x.y.z**
Done
完毕
回答by Pramod Kumar
INSTALL PM2 OFFLINE:-
离线安装 PM2:-
Tested on Node-v6.10.3 and Npm-3.10.10 on RHEL-7
在 Node-v6.10.3 和 Npm-3.10.10 上在 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
回答by commenthol
I created offline-npmfor getting all the dependencies installed in a clean way. For modules without the use of node-gyp everything should work as described.
我创建了offline-npm来以干净的方式安装所有依赖项。对于不使用 node-gyp 的模块,一切都应该如描述的那样工作。
If you require node-gyp(which is usually installed online) consider copying ~/.node-gyp to that offline machine.
如果您需要node-gyp(通常在线安装),请考虑将 ~/.node-gyp 复制到该离线机器。
回答by gkns
回答by f0ster
List the dependencies in bundledDependenciesin your package.json, and then run npm packto create a tarball. Get that over to the other machine, and either npm install <tarball>, or just crack it open manually.
列出bundledDependenciespackage.json 中的依赖项,然后运行npm pack以创建 tarball。把它转移到另一台机器上,或者npm install <tarball>,或者只是手动打开它。
回答by assafmo
Using Yarn:
使用纱线:
On the internet machine (configure local cache location):
yarn config set yarn-offline-mirror ~/yarn-offline-mirror/On the offline machine (configure local cache location):
yarn config set yarn-offline-mirror ~/yarn-offline-mirror/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>)On the offline machine, add it to your path. E.g.:
echo 'export PATH=$PATH:'"$(yarn global bin)" >> ~/.bashrc source ~/.bashrc # reloadOn the internet machine, download
forever's dependencies:mkdir new-cli-forever/ cd new-cli-forever/ yarn add foreverThen copy
new-cli-forever/yarn.lockand~/yarn-offline-mirror/to the offline machine. (rm -rf new-cli-forever/is ok now.)On the offline machine, install
foreverfrom 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
在互联网机器上(配置本地缓存位置):
yarn config set yarn-offline-mirror ~/yarn-offline-mirror/在离线机器上(配置本地缓存位置):
yarn config set yarn-offline-mirror ~/yarn-offline-mirror/在离线机器上,找出全局安装位置在哪里:
yarn global bin(或设置它
yarn config set prefix <file_path>)在离线机器上,将其添加到您的路径中。例如:
echo 'export PATH=$PATH:'"$(yarn global bin)" >> ~/.bashrc source ~/.bashrc # reload在互联网机器上,下载
forever的依赖项:mkdir new-cli-forever/ cd new-cli-forever/ yarn add forever然后复制
new-cli-forever/yarn.lock并~/yarn-offline-mirror/到离线机器。(rm -rf new-cli-forever/现在可以了。)在离线机器上,
forever从本地缓存安装: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
有关更多信息,请参阅我的帖子:https: //assafmo.github.io/2018/04/11/yarn-offline.html
回答by Sandeep Kanabar
On your local machine or any machine that has internet connection, do
在您的本地计算机或任何具有 Internet 连接的计算机上,执行
npm install npm-bundle -g
npm install forever -g
Now, go to cd /usr/local/lib/node_modules/foreverand do
现在,cd /usr/local/lib/node_modules/forever去做
npm-bundle
It will create a .tgzfile. Now scp/ftpthat .tgzfile to the offline server and do
它将创建一个.tgz文件。现在scp/ftp该.tgz文件到离线服务器并执行
npm install forever -g
Reference: This blog
参考:本博客
回答by jpenna
npmbox is outdated
npmbox 已过时
Use npm packcommand (npm docs), no need to install anything.
使用npm pack命令(npm docs),无需安装任何东西。
Single package
单包
If you want only one package (for example, forever), you can run:
如果您只需要一个包(例如,forever),您可以运行:
npm pack forever
this command will fetch it to the cache, and then copy the tarball to the current working directory as -.tgz
此命令会将其提取到缓存中,然后将 tarball 以 -.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.jsonnamed bundleDependenciesand list all dependecies you need in this field.
如果您希望离线安装整个项目,请在您的package.json命名中包含一个 poperty,bundleDependencies并在此字段中列出您需要的所有依赖项。
// 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.
然后运行npm pack。
It will create a .tgzfile of your whole project and dependencias.
它将创建.tgz整个项目和依赖项的文件。
You just have to copy it to the offline server and untar.
您只需将其复制到离线服务器并解压缩即可。

