Home > other >  how to open node in a new terminal from cmd?
how to open node in a new terminal from cmd?

Time:11-11

when I type start node into the cmd it starts up a new node terminal like this:

but how do I get it to run a script in the node terminal and close it after it's done?

maybe something like this?

start node "index.js" (when I try this it opens a terminal then immediately closes)

index.js:

console.log('hello there!');

await new Promise(r=>setTimeout(r,3000));

CodePudding user response:

Top-level awaits aren't supported in Node versions prior to 14.18.0; there is a helpful discussion on GitHub as to why.

Since you're running v14.17.6, you'll need to wrap your code in an anonymous async function, then invoke it with ():

(async () => {
    console.log('hello there!');
    await new Promise(r=>setTimeout(r,3000));
})();

The resulting Node terminal will await for your timeout period before being closed.

(It may also behoove those reading this answer to ensure that they are correctly referencing the script they wish to execute relative to their command line's current working directory; see comment below by @cakelover.)

  • Related