Nodejs和npm模块离线安装

3
我在一家有企业代理的公司工作。我可以安装nodejs,但是无法安装任何npm模块。我尝试了很多方法,不同的代理设置,但都没有起作用。Nodejs无法连接到互联网获取我需要的模块。基本上,我正在尝试在这台计算机上设置Cordova和Ionic。
我想知道是否有离线安装的方法?我的意思是,是否有办法将这些文件带到USB驱动器或其他设备中,然后进行安装?
这可能吗?
谢谢大家 :)
4个回答

3

是的,您可以使用Yarn在没有互联网连接的情况下安装软件包。
例如,全局安装ioniccordova

  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 ionic's and cordova's dependencies:

    mkdir new-cli-ionic-cordova/
    cd new-cli-ionic-cordova/
    yarn add ionic cordova
    

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

  6. On the offline machine, install ionic and cordova from local cache:

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

您也可以使用类似的过程将软件包安装为单个项目的依赖项。有关更多信息,请参阅我的帖子:https://assafmo.github.io/2018/04/11/yarn-offline.html


对我来说,这篇博客文章包含了正确的步骤,但是答案本身并没有;也许你更新了博客文章,但忘记回来修改了?你在这里没有提到 package.json 文件,我可以确认它是必需的。 - TamaMcGlinn

0

首先,我建议您尝试让npm-config在代理服务器上正常工作。请与您的IT部门联系,了解如何连接到https://www.npmjs.com/package/注册表。如果您还没有找到它们,可能以下链接会有所帮助:

如果这些方法都无法解决问题,那么您可以考虑设置本地软件包注册表。也许Sinopia可以帮助您。但是……

  • A) 当你的IT部门不允许你通过网络连接到官方注册表时,为什么他们会允许你通过USB这样做呢?
  • B) 这将引入大量的工作开销,以保持你的本地软件包注册表与官方注册表同步。而且这个努力显然必须在你的工作场所之外进行。

还有其他想法吗?


我尝试了链接中提供的所有代理配置设置,但它们都没有起作用。我确定我们的IT团队不会关心这个问题,因为这只是我自己想做的一件事情,只是一种爱好...我以为我可以耍聪明,离线安装整个东西,但显然这似乎不可行。非常感谢您上面提供的解决方案。非常感激。 - Amrit Sohal
关于问题A - IT部门普遍采用了激进的内容扫描和阻止策略,导致从互联网安装任何东西都变得不可能,因为代理服务器对内容进行了过多的干扰。因此,IT部门自己需要使用USB解决方案来保持公司运营... - 576i
嗨@576i,你的意思是:IT部门更喜欢从难以验证的随机USB设备中获取安装程序,而不是使用带有签名和可信证书的原始来源?这难道不会让整个公司网络处于风险之中吗? - Konstantin A. Magg

0

您可以通过代理使npm正常工作以解决问题。为此,设置HTTP_PROXY和HTTPS_PROXY环境变量非常重要。

对于HTTP_PROXY,您可以使用

npm config set proxy http://proxy_host:port

对于 HTTPS_PROXY,您可以尝试使用这个

 npm config set https-proxy http://proxy.company.com:8080

0
  1. 离线安装Node.js:

步骤1:下载Node.js:https://nodejs.org/en/download/

步骤2:tar xvf node-v16.17.0-linux-x64.tar.xz

步骤3:添加到环境变量中:

vim /etc/profile.d/nodejs.sh:
#!/bin/sh
export PATH=/YOUR-PATH-TO/node-v12.16.2-linux-s390x/bin:$PATH

or 

vim ~/.bash_profile:
export PATH=/YOUR-PATH-TO/node-v12.16.2-linux-s390x/bin:$PATH
  • 离线安装npm包或node_modules:
  • 方法1:

    on machine A which has internet access:
    npm install all the packages needed
    upload the node_modules/* to /YOUR-PATH-TO/node-v12.16.2-linux-s390x/lib/node_modules/
    

    方法二:

    on machine A which has internet access:
    >npm install -g npm-bundle
    >npm install -g eslint
    >npm-bundle eslint
    eslint-7.8.1.tgz
    on the target machine which has no internet access:
    >npm install -g ./eslint-7.8.1.tgz
    

    参考JS概述


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接