如何在没有互联网连接的情况下在本地安装 NodeJS 项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23250805/
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 NodeJS project locally without internet connection?
提问by Katya S
I have a project which I will have to deploy to client Windows systems where it will not be possible to connect to internet. I currently have a folder in D:\NODE which contains node.exe and npm.cmd and a node_modules folder. To be able to run node from command line I have added D:\NODE to PATH variable.
我有一个项目,我必须将其部署到无法连接到 Internet 的客户端 Windows 系统。我目前在 D:\NODE 中有一个文件夹,其中包含 node.exe 和 npm.cmd 以及一个 node_modules 文件夹。为了能够从命令行运行节点,我已将 D:\NODE 添加到 PATH 变量。
I can have most of the modules installed locally inside node_modules of my project. However there's one - node-windows - which needs to be installed globally to work.
我可以在我的项目的 node_modules 中本地安装大多数模块。然而,有一个 - node-windows - 需要全局安装才能工作。
Following suggestion below I went to node-windows (installed globally) and packaged it up (npm pack), which created a tarball. I have then copied that file with my project and tried to install it on the test machine globally like this: npm install -g node-windows-0.1.5.tgz
按照下面的建议,我转到 node-windows(全局安装)并将其打包 ( npm pack),它创建了一个 tarball。然后我用我的项目复制了那个文件,并尝试像这样全局地将它安装在测试机器上:npm install -g node-windows-0.1.5.tgz
I can see that it got installed in the global directory. However when I try to run the command which uses this module it complains that it cannot find it: Error: Cannot find module 'node-windows'
我可以看到它安装在全局目录中。但是,当我尝试运行使用此模块的命令时,它抱怨找不到它:Error: Cannot find module 'node-windows'
When I list the modules (npm list -g) it is clearly there in the list...
当我列出模块 ( npm list -g) 时,它显然在列表中...
What do you think? And thank you.
你怎么认为?谢谢。
回答by aludvigsen
You can install packages on a system without internet connection by packing them using built-in functionality in npm. This way, the node moduleswill be installed properly.
您可以在没有互联网连接的系统上安装软件包,方法是使用npm 中的内置功能打包它们。这样,节点模块将被正确安装。
- Create a package.json.
- In your package.json, list all the modules you need under
bundledDependencies(docs on npm). - Run
npm installto install your node files before packing. - Create a tarball with
npm pack. - Copy the tarball over to the machine without internet connection.
- Install the modules with
npm install <filename>.
- 创建一个package.json。
- 在您的package.json 中,列出您需要的所有模块
bundledDependencies(npm 上的文档)。 npm install在打包之前运行以安装您的节点文件。- 使用
npm pack. - 将 tarball 复制到没有互联网连接的机器上。
- 安装模块
npm install <filename>。
Update
更新
Regarding your comments, it looks like your globally installed node modulesisn't found.
关于您的评论,似乎未找到您全局安装的节点模块。
Try using the npm linkcommand (docs on npm link):
尝试使用npm link命令(npm 链接上的文档):
cd yourAppFoldernpm link node-windows
cd yourAppFoldernpm link node-windows
回答by ali khezri
1 - In system with internet access install module with this command:
1 - 在具有 Internet 访问权限的系统中,使用以下命令安装模块:
npm install [module name]
2 - go to %userprofile%\AppData\Roaming\npm\node_modules[module name]\
(e.g C:\Users\janson\AppData\Roaming\npm\node_modules\grunt-cli)
3 - run npm pack
4 - this should result in a [module name]-x.y.z.tgz file
5 - run npm i -g [module name]-x.y.z.tgzin offline system
2 - 转到 %userprofile%\AppData\Roaming\npm\node_modules[module name]\(例如 C:\Users\janson\AppData\Roaming\npm\node_modules\grunt-cli)
3 - 运行npm pack
4 - 这应该导致a [模块名称]-xyztgz 文件
5 -npm i -g [module name]-x.y.z.tgz在离线系统中运行

