如何解决运行 Node JS 应用程序时的 NPM 代理问题

Nil*_*esh 5 javascript proxy node.js npm

我已经安装了Noed JS和NPM。我已经从 git 克隆了存储库并尝试在本地运行该应用程序。但我在运行应用程序时面临以下代理问题。

npm install nodemon
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to https://registry.npmjs.com/nodemon failed, reason: getaddrinfo ENOTFOUND 28
npm ERR! network This is a problem 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'`
Run Code Online (Sandbox Code Playgroud)

我已检查代理并将其设置为以下网络。

Pinging proxy.internal.domainname.com with 32 bytes of data:
Reply from <ip address="">: bytes=32 time=2ms TTL=247
Reply from <ip address="">: bytes=32 time=2ms TTL=247
Reply from <ip address="">: bytes=32 time=2ms TTL=247
Reply from <ip address="">: bytes=32 time=2ms TTL=247
Ping statistics for <ip address="">:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 2ms, Maximum = 2ms, Average = 2ms
Run Code Online (Sandbox Code Playgroud)

我尝试https://registry.npmjs.com/nodemon使用以下命令将代理设置为。

npm config set registry "http://registry.npmjs.org/ 
Run Code Online (Sandbox Code Playgroud)

当我在此之后运行 npm install 时,再次遇到相同的错误。

npm --proxy http://registry.npmjs.org/  install npm
npm ERR! code ENOTFOUND
npm ERR! errno ENOTFOUND
npm ERR! network request to http://registry.npmjs.org/npm failed, reason: getaddrinfo ENOTFOUND 28
npm ERR! network This is a problem 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'`
Run Code Online (Sandbox Code Playgroud)

我已经尝试了谷歌的许多其他解决方法,但仍然面临相同的代理问题。我只是想在本地运行这个应用程序,以便我可以在部署之前在本地测试我的代码。我在这里错过了什么吗?

下面是我的示例 package.json 文件。

{
  "name": "abc",
  "version": "0.0.1",
  "description": "abc",
  "main": "dist/index.js",
  "engines": {
    "node": "8.0.0",
    "npm": "5.0.0"
  },
  "scripts": {
    "prestart": "npm run -s build",
    "start": "node dist/index.js",
    "dev": "nodemon src/index.js --exec \"node -r dotenv/config -r babel-register\" localdev",
    "clean": "rimraf dist && rimraf -p",
    "build": "npm run clean && mkdir -p dist && babel src -s -D -d dist",
    "test": "jest --watch",
    "lint": "esw -w src test"
  },
  "keywords": [
    "express",
    "babel",
    "es6",
    "es2015",
    "es2016",
    "es2017",
    "eslint"
  ],
  "author": "abc",
  "license": "LICENSED",
  "dependencies": {
    "babel-cli": "6.26.0",
    "babel-plugin-transform-object-rest-spread": "6.26.0",
    "babel-preset-env": "1.6.1",
    "cors": "2.8.5",
    "express": "4.16.4",
    "js-yaml": "3.12.2",
    "mssql": "5.0.0",
    "winston": "3.1.0",
    "winston-daily-rotate-file": "3.5.1"
  },
  "devDependencies": {
    "babel-eslint": "7.2.3",
    "babel-jest": "21.0.2",
    "babel-register": "6.24.1",
    "dotenv": "4.0.0",
    "eslint": "4.10.0",
    "eslint-config-airbnb-base": "12.1.0",
    "eslint-plugin-import": "2.8.0",
    "eslint-plugin-jest": "21.0.2",
    "eslint-watch": "3.1.0",
    "nodemon": "1.18.10",
    "rimraf": "2.6.3"
  },
  "babel": {
    "presets": [
      [
        "env",
        {
          "targets": {
            "node": "current"
          }
        }
      ]
    ],
    "plugins": [
      "transform-object-rest-spread"
    ]
  },
  "eslintConfig": {
    "parser": "babel-eslint",
    "plugins": [
      "import",
      "jest"
    ],
    "parserOptions": {
      "ecmaVersion": 2017,
      "sourceType": "module"
    },
    "env": {
      "node": true,
      "jest": true
    },
    "extends": [
      "eslint:recommended"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

运行该npm config ls命令将返回以下结果:

{
  "name": "abc",
  "version": "0.0.1",
  "description": "abc",
  "main": "dist/index.js",
  "engines": {
    "node": "8.0.0",
    "npm": "5.0.0"
  },
  "scripts": {
    "prestart": "npm run -s build",
    "start": "node dist/index.js",
    "dev": "nodemon src/index.js --exec \"node -r dotenv/config -r babel-register\" localdev",
    "clean": "rimraf dist && rimraf -p",
    "build": "npm run clean && mkdir -p dist && babel src -s -D -d dist",
    "test": "jest --watch",
    "lint": "esw -w src test"
  },
  "keywords": [
    "express",
    "babel",
    "es6",
    "es2015",
    "es2016",
    "es2017",
    "eslint"
  ],
  "author": "abc",
  "license": "LICENSED",
  "dependencies": {
    "babel-cli": "6.26.0",
    "babel-plugin-transform-object-rest-spread": "6.26.0",
    "babel-preset-env": "1.6.1",
    "cors": "2.8.5",
    "express": "4.16.4",
    "js-yaml": "3.12.2",
    "mssql": "5.0.0",
    "winston": "3.1.0",
    "winston-daily-rotate-file": "3.5.1"
  },
  "devDependencies": {
    "babel-eslint": "7.2.3",
    "babel-jest": "21.0.2",
    "babel-register": "6.24.1",
    "dotenv": "4.0.0",
    "eslint": "4.10.0",
    "eslint-config-airbnb-base": "12.1.0",
    "eslint-plugin-import": "2.8.0",
    "eslint-plugin-jest": "21.0.2",
    "eslint-watch": "3.1.0",
    "nodemon": "1.18.10",
    "rimraf": "2.6.3"
  },
  "babel": {
    "presets": [
      [
        "env",
        {
          "targets": {
            "node": "current"
          }
        }
      ]
    ],
    "plugins": [
      "transform-object-rest-spread"
    ]
  },
  "eslintConfig": {
    "parser": "babel-eslint",
    "plugins": [
      "import",
      "jest"
    ],
    "parserOptions": {
      "ecmaVersion": 2017,
      "sourceType": "module"
    },
    "env": {
      "node": true,
      "jest": true
    },
    "extends": [
      "eslint:recommended"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

编辑2:

我现在运行以下命令来删除错误的配置密钥,即该https://registry.npmjs.com/nodemon = ""部分:

; cli configs
metrics-registry = "http://registry.npmjs.org/async-each"
scope = ""
user-agent = "npm/6.13.4 node/v12.14.0 win32 x64"

; userconfig C:\Users\username\.npmrc
https-proxy = "http://registry.npmjs.org/"
https://registry.npmjs.com/nodemon = ""
key = ""
registry = "http://registry.npmjs.org/"
strict-ssl = false

; builtin config undefined
prefix = "C:\\Users\\username\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = H:\IRB PMX Services\core\src
; HOME = C:\Users\username
; "npm config ls -l" to show all defaults.
Run Code Online (Sandbox Code Playgroud)

并再次运行npm install命令。但再次出现代理错误,如下所示:

npm config delete https://registry.npmjs.com/nodemon
Run Code Online (Sandbox Code Playgroud)

Nil*_*esh 9

我已通过以下步骤解决了此问题:

\n\n
    \n
  1. 确保您有互联网连接: ping 8.8.8.8
  2. \n
  3. 确保您的 DNS 解析器正常工作: ping www.google.com
  4. \n
  5. 确保您可以访问registry.npmjs.org:pingregistry.npmjs.org
  6. \n
  7. 确保您可以通过 https 连接。
  8. \n
\n\n

在 npm 命令提示符或 Visual Studio Code 终端中运行以下命令。

\n\n

npm config rm proxy

\n\n

npm config rm https-proxy

\n\n

在浏览器中尝试: https: //registry.npmjs.org/

\n\n

确保您正在尝试连接到:

\n\n
\n

注册中心.npmjs.org

\n
\n\n

而不是:

\n\n
\n

“registry.npmjs.orgregistry.npmjs.org:443”

\n
\n\n

使用以下命令设置注册表。

\n\n

npm config set registry https://registry.npmjs.org/

\n\n

如果您使用代理来访问 Web,那么您还必须配置 npm 才能使用它。使用:

\n\n

npm config set proxy http://username:password@proxyname:8080

\n\n

npm config set https-proxy http://username:password@proxyname:8080

\n\n

设置以上两个值时要记住的要点:

\n\n
    \n
  1. proxyname 您需要与您公司的网络团队核实。在上面 2 个值中输入该代理 URL。
  2. \n
  3. 如果您的密码包含任何特殊字符,请将其替换为编码字符。在此命令中,您可以\xe2\x80\x99t 提供带有特殊字符的密码。仅替换特殊字符而不是 .(点)。\n\n
    \n

    例如:密码是:Welcome@12# 那么它将像Welcome%4012%23。

    \n
  4. \n
\n\n

https://www.w3schools.com/tags/ref_urlencode.asp

\n\n

然后尝试运行 npm install 命令,它应该可以工作。

\n