【问题标题】:"npm start" shows "Unhandled promise rejection" error“npm start”显示“未处理的承诺拒绝”错误
【发布时间】:2017-09-23 08:24:04
【问题描述】:

我是 NodeJS 和 NPM 的新手。

当我在 NodeJS 项目中运行 npm start 时,出现以下错误:

Starting the development server...

(node:9417) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Exited with code 3

这个错误是什么意思? 这个问题应该怎么调试?

$ grep start package.json 
    "start": "react-scripts start",

$ npm -v
3.10.10
$ node -v
v6.10.1

$ npm ls react-scripts
reference-apps@2.3.1 /home/li/sample
└── react-scripts@0.5.1  

【问题讨论】:

    标签: node.js reactjs npm npm-start react-scripts


    【解决方案1】:

    我猜你的代码如下所示

    new Promise(function(resolve, reject){
        reject(0)
    }).then()
    

    当你运行上面的代码时,你会得到“Unhandled Promise Rejection”。

    使用 Promise/A+ 标准 #point-21。 Promise 必须提供 then 方法来访问其当前或最终的值或原因。

    你最好写如下代码

    promise.then(onFulfilled, onRejected)
    

    另一种避免该问题的方法,您可以使用进程监听unhandledRejection 事件

    process.on('unhandledRejection', (reason, p) => {
      console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
      // application specific logging, throwing an error, or other logic here
    });  
    

    【讨论】:

    • 有没有办法知道这个错误来自哪里?代码中有很多reject()。我不确定是哪一个导致了错误。
    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    相关资源
    最近更新 更多