从命令行运行`node index.js`是可行的,但是当我在package.json中使用启动脚本时,出现了EADDRINUSE / usr / bin / local / node错误

时间:2018-10-07 16:36:03

标签: node.js heroku server yarnpkg

我可以直接从命令行运行node index.js,但是当我运行具有相同命令的启动脚本时,会出现错误。

以下是相对服务器信息:

const PORT = process.env.NODE || 3000

app.listen(PORT, () => console.log(`Server running on ${PORT}`))

这是package.json脚本:

"scripts": {
    "start": "node index.js"
},

这是从项目根目录运行的终端输出,无需脚本即可运行它:

 > node index.js
 Server running on port 3000

以下是使用脚本运行它的终端输出:

>yarn start
yarn run v1.10.1
$ node index.js
events.js:183
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE /usr/local/bin/node
    at Server.setupListenHandle [as _listen2] (net.js:1343:19)
    at listenInCluster (net.js:1401:12)
    at Server.listen (net.js:1496:5)
    at Function.listen (/Users/mmelv/Workspace/Projects/Reportably/server/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/mmelv/Workspace/Projects/Reportably/server/index.js:29:5)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Function.Module.runMain (module.js:694:10)
    at startup (bootstrap_node.js:204:16)
    at bootstrap_node.js:625:3
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

这使我震惊了几个小时。我尝试过sudo killall nodelsof -i tcp:3000什么也没有返回。我已经重新安装并升级了节点。

此外,当我尝试将其部署到Heroku时,也会遇到相同的错误:

2018-10-07T15:47:56.000000+00:00 app[api]: Build succeeded
2018-10-07T15:47:58.569471+00:00 heroku[web.1]: Starting process with command `npm start`
2018-10-07T15:48:01.927982+00:00 app[web.1]:
2018-10-07T15:48:01.928007+00:00 app[web.1]: > server@1.0.0 start /app
2018-10-07T15:48:01.928009+00:00 app[web.1]: > node index.js
2018-10-07T15:48:01.928010+00:00 app[web.1]:
2018-10-07T15:48:02.936929+00:00 app[web.1]: events.js:183
2018-10-07T15:48:02.936974+00:00 app[web.1]: throw er; // Unhandled 'error' event
2018-10-07T15:48:02.936976+00:00 app[web.1]: ^
2018-10-07T15:48:02.936977+00:00 app[web.1]:
2018-10-07T15:48:02.936979+00:00 app[web.1]: Error: listen EADDRINUSE /app/.heroku/node/bin/node
2018-10-07T15:48:02.936981+00:00 app[web.1]: at Object._errnoException (util.js:992:11)
2018-10-07T15:48:02.936982+00:00 app[web.1]: at _exceptionWithHostPort (util.js:1014:20)
2018-10-07T15:48:02.936984+00:00 app[web.1]: at Server.setupListenHandle [as _listen2] (net.js:1338:19)
2018-10-07T15:48:02.936985+00:00 app[web.1]: at listenInCluster (net.js:1396:12)
2018-10-07T15:48:02.936987+00:00 app[web.1]: at Server.listen (net.js:1491:5)
2018-10-07T15:48:02.936989+00:00 app[web.1]: at Function.listen (/app/node_modules/express/lib/application.js:618:24)
2018-10-07T15:48:02.936990+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:29:5)
2018-10-07T15:48:02.936992+00:00 app[web.1]: at Module._compile (module.js:652:30)
2018-10-07T15:48:02.936993+00:00 app[web.1]: at Object.Module._extensions..js (module.js:663:10)
2018-10-07T15:48:02.936995+00:00 app[web.1]: at Module.load (module.js:565:32)
2018-10-07T15:48:02.936996+00:00 app[web.1]: at tryModuleLoad (module.js:505:12)
2018-10-07T15:48:02.936998+00:00 app[web.1]: at Function.Module._load (module.js:497:3)
2018-10-07T15:48:02.936999+00:00 app[web.1]: at Function.Module.runMain (module.js:693:10)
2018-10-07T15:48:02.937001+00:00 app[web.1]: at startup (bootstrap_node.js:191:16)
2018-10-07T15:48:02.937003+00:00 app[web.1]: at bootstrap_node.js:612:3
2018-10-07T15:48:03.081497+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2018-10-07T15:48:03.082095+00:00 app[web.1]: npm ERR! errno 1
2018-10-07T15:48:03.083978+00:00 app[web.1]: npm ERR! server@1.0.0 start: `node index.js`
2018-10-07T15:48:03.088003+00:00 app[web.1]: npm ERR! Exit status 1
2018-10-07T15:48:03.088362+00:00 app[web.1]: npm ERR!
2018-10-07T15:48:03.088616+00:00 app[web.1]: npm ERR! Failed at the server@1.0.0 start script.
2018-10-07T15:48:03.088965+00:00 app[web.1]: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

我不确定该怎么办。为什么我可以运行node index.js而不执行相同功能的启动脚本?

1 个答案:

答案 0 :(得分:0)

我不得不将process.env.NODE更改为process.env.PORT ... 我知道,我知道...