Node – ReferenceError:

  javascript, node.js

iam new starter at node.js , i write my code right but still got error promise is not defined after run node test.js , please i hope some experience developer have solution for this error here is the code for my 2 files include in node js project :

first file is >> test.js

const genRandom = require('./test2.js');



async function syncFunc() {
    try {
        startTest = await genRandom();
        console.log(` it's run and your random number is ${startTest} `);
    } catch (error) {
        console.log(error);
        console.log('error happen ');
    }
}

syncFunc();

second file >> test2.js

let genRandom = () => {
    new promise(function(resolve, reject) {
        console.log('start promise avoking function');
        setTimeout(() => {
            console.log('num is genrate');
            let randomNum = Math.floor(Math.random() * 8);
            resolve('randomNum');
        }, 2000);
    })
};


module.exports = genRandom;

file package.json

{
    "name": "project1",
    "version": "1.0.0",
    "description": "",
    "main": "test.js",
    "scripts": {
        "test": "echo "Error: no test specified" && exit 1"
    },
    "author": "karim all",
    "license": "ISC",
    "dependencies": {
        "es6-promise": "^4.2.8",
        "node": "^16.3.0",
        "promise": "^8.1.0"
    }
}

thanks alot for helping <3

Source: Ask Javascript Questions

LEAVE A COMMENT