node.js 为什么我会得到“未定义承诺”。Node v5.7.0 上的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35639924/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:58:08  来源:igfitidea点击:

Why would I get a "Promise is not defined." error on Node v5.7.0

node.jspostcss

提问by

Im using autoprefixerwith postcssand after moving to a new linux server something must be wrong but I cant figure out what this one could be. I'm getting the error:

我在postcss 中使用autoprefixer,在移动到新的 linux 服务器后,一定有问题,但我无法弄清楚这可能是什么。我收到错误:

/home/ec2-user/Enviziion/Muveoo/Server/node_modules/postcss/lib/lazy-result.js:157
        this.processing = new Promise(function (resolve, reject) {
                              ^
ReferenceError: Promise is not defined

Which is triggered by:

这是由以下触发的:

var autoprefixer = require('autoprefixer');
var postCSS = require('postcss');

function prefix(css, res, type, fullPath) {
    postCSS([autoprefixer]).process(css).then(function(result) {
        var css = result.css;
        var length = css.length;
        res.writeHead(200, {
            'Content-Length' : length,
            'Content-Type' : type
        });
        res.write(css);
        res.end();
    });
}

I researched this problem but all of the occurrences of the issue seem to be for extremely early versions of node.js, for example:

我研究了这个问题,但所有出现的问题似乎都是针对 node.js 的极早期版本,例如:

And the solution always seems to be "Update Node".

解决方案似乎总是“更新节点”。

But mine seems to be up to date:

但我的似乎是最新的:

[ec2-user@ip-172-31-22-79 Server]$ node -v
v5.7.0

What might my issue be here?

我的问题可能是什么?

回答by

I cant answer why this is happening, but after reinstalling all npm packages, I still had the same error, so I used this very old solution to "monkeypatch" Promises into node:

我无法回答为什么会发生这种情况,但是在重新安装所有 npm 包后,我仍然遇到相同的错误,因此我使用了这个非常旧的解决方案将 Promises 用于“monkeypatch”到节点中:

npm install es6-promise

and then add the code:

然后添加代码:

var Promise = require('es6-promise').Promise;

And that "solved" the problem.

这“解决”了问题。

Edit (a year later):people are still up-voting this answer so I just want to point out to anyone encountering this, the question has gotten a ton of views and seems to be a common issue considering how weird it is - a deduction I later made was that the only reasonable explanation is that one of my libraries (perhaps many libraries do this same thing) built before promises were introduce, had them implemented manually and caused a conflict when Node was updated to support promises officially.

编辑(一年后):人们仍然对这个答案进行投票,所以我只想向遇到此问题的任何人指出,这个问题已经获得了大量观点,并且考虑到它有多奇怪似乎是一个常见问题 - 推论我后来做出的唯一合理解释是,我的一个库(可能很多库都做同样的事情)在引入 promise 之前构建,手动实现它们并在 Node 更新以正式支持 promise 时导致冲突。

You might be running a legacy version of a maintained library for whatever reason (sometimes necessary to avoid maintenance of your older servers) or be running a current version of an old library that's no longer maintained. Either way, this solution seems to work.

无论出于何种原因(有时是避免维护旧服务器所必需的),您可能正在运行已维护库的旧版本,或者正在运行不再维护的旧库的当前版本。无论哪种方式,这个解决方案似乎都有效。

回答by Praveen Bijja

Upgrading node to latest version(v4.5.0) did resolved this issue.

将节点升级到最新版本(v4.5.0)确实解决了这个问题。