首页 > 解决方案 > 由于缺少“注册表”,Npm 全局安装在 VM 中失败?

问题描述

我在合作环境中使用没有免费互联网访问权限的虚拟机。我可以添加一个特定的注册表链接,这样即使互联网访问受限,使用 npm install 也不会出现任何问题。但是,当尝试使用 npm -g install 执行相同操作时,它不起作用,似乎问题在于它不使用我添加的注册表。

npm ERR! Linux 4.19.49-1560253987
npm ERR! argv "/usr/bin/node" "/bin/npm" "-g" "install" "ganache-cli"
npm ERR! node v6.16.0
npm ERR! npm  v3.10.10
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! syscall getaddrinfo

npm ERR! network getaddrinfo ENOTFOUND registry.npmjs.org registry.npmjs.org:443
npm ERR! network This is most likely not a problem with npm itself
npm ERR! network and is related to network connectivity.
npm ERR! network In most cases you are behind a proxy or have bad network settings.
npm ERR! network
npm ERR! network If you are behind a proxy, please make sure that the
npm ERR! network 'proxy' config is set properly.  See: 'npm help config'

因为这也是我在没有添加注册表的情况下使用 npm install 时收到的消息。只是它现在只发生在全局安装中。

我会尝试在本地使用它,但它似乎需要全局安装,因为当我尝试通过进入节点模块并执行脚本来使用命令行脚本时,它给了我:(松露是名称脚本)

$ node truffle -version
/home/xxxxx/node_modules/truffle/build/cli.bundled.js:67600
  async execute() {
        ^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.runMain (module.js:611:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:160:9)
[xxxxx.bin]$ node truffle --version
/home/xxxxx/node_modules/truffle/build/cli.bundled.js:67600
  async execute() {
        ^^^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.runMain (module.js:611:10)
    at run (bootstrap_node.js:394:7)
    at startup (bootstrap_node.js:160:9)

并使用我尝试安装的另一个包

$ node ganache-cli -g
/home/xxxxx/node_modules/ganache-cli/build/ganache-core.node.cli.js:47
!function(){var r,a={};function i(){}function o(e){return e}function c(e){return!!e}function s(e){return!e}var f="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||this;function d(e){return function(){if(null===e)throw new Error("Callback was already called.");e.apply(this,arguments),e=null}}function u(e){return function(){null!==e&&(e.apply(this,arguments),e=null)}}null!=f&&(r=f.async),a.noConflict=function(){return f.async=r,a};var l=Object.prototype.toString,b=Array.isArray||function(e){return"[object Array]"===l.call(e)};function p(e){return b(e)||"number"==typeof e.length&&e.length>=0&&e.length%1==0}function h(e,t){for(var r=-1,n=e.length;++r<n;)t(e[r],r,e)}function g(e,t){for(var r=-1,n=e.length,a=Array(n);++r<n;)a[r]=t(e[r],r,e);return a}function m(e){return g(Array(e),function(e,t){return t})}function v(e,t){for(var r=0;r<e.length;r++)if(e[r]===t)return r;return-1}var x=Objec

SyntaxError: missing ) after argument list
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/home/xxxxx/node_modules/ganache-cli/cli.js:12:13)

所以这也行不通。

有没有办法添加注册表,以便在我全局安装某些东西时使用它?

是否可以做其他事情来使其在本地工作,或者可能只是我正在工作的奇怪环境引起的其他问题?

标签: npmcentos7

解决方案


根本原因很可能是您的虚拟机时钟不同步,所以我会先检查一下。这是在centos上对我有用的:

第一次尝试:

yum install ntp
chkconfig ntpd on
ntpdate pool.ntp.org
service ntpd start

如果这不能解决您的问题,您可以(我警告您这不安全)尝试更改 npm 的配置。

$ npm config set strict-ssl false
$ npm config set unsafe-perm true
$ npm config set registry http://registry.npmjs.org/

推荐阅读