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

3

13

Im using autoprefixer with postcss and 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:

/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:

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?

Trek answered 25/2, 2016 at 22:44 Comment(9)
could you try this command : which nodeBang
@Bang ~/.nvm/versions/node/v5.7.0/bin/nodeTrek
try this solution : #32490828Bang
@Bang Im not using gulp but I assume you want me to try deleting my node moldules folder and reinstalling the packages, Ill try that.Trek
@Bang Ok I tried deleting the node modules folder, and reinstalling all the required packages, and I've even tried installing es6-promise. The error hasnt changed. Edit: Nevermind, using es6-promise did work.Trek
Where did you get the node executable from? Try sticking to official sources, like download links on node site.Sheugh
@YurySolovyov I'm not very experienced with linux systems but iirc, I used "yum" to install node. Not sure if that's a thing or not, but I dont recall (long time ago now) uploading / installing node manually to the Amazon EC2 server I was using at the time.Trek
@Viziionary could you please post your package.json at the time of the error. I'm trying to do a post-mortem on this, and I would like to know the version of the modules you were using.Arta
@RefaelAckermann Too long ago, dont have the project anymore. I would suspect that perhaps some library involved messed up the promises, thats the only way I can think of it being possible. I notice a ton of people visit this question though - an insane number for a question like this, so maybe it really is some bug with postcss or else a popular library like socket.io which I wouldve been using.Trek
T
23

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 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.

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.

Trek answered 25/2, 2016 at 23:33 Comment(2)
Thanks, that worked great (I had the same problem). I used the "-g" option for the npm install command to make it global. Do you know if that will be a problem?Sweetie
I recommend installing with npm install es6-promise because the version of Promise is different in each caseElectrocute
S
1

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

Statecraft answered 31/3, 2017 at 18:13 Comment(0)
S
1

just to add another answer to this, one which helped me in my case.

if you are using eslint, ensure to add "es6": true under the env config.

    "env": {
        "browser": false,
        "node": true,
        "commonjs": true,
        "es6": true  // add this in
    },

then Promises should no longer show up as not defined

Stamata answered 6/1, 2023 at 17:10 Comment(1)
I'm using eslint, and this removes the error noise. Nice.Therapist

© 2022 - 2024 — McMap. All rights reserved.