简体   繁体   中英

Run another Node JS file in command prompt without closing and restarting it

Being New in NodeJS Whenever I run my node JS file in command prompt. I have to close the console window and type the same procedure again. is there any way that i have to not run again and again command prompt.

在命令提示符下使用ctrl+c ,您可以使用同一命令提示符窗口来运行同一程序。

For example there are two node.js files.

  1. nodeOne.js
  2. nodeTwo.js

If, these nodes need a port to run, configure both on different port numbers.

eg 8080 for nodeOne.js and 8081 for nodeTwo.js

Now, open two instances of 'cmd' and execute both nodes separately.

在此处输入图片说明

I'd use "start node whatever.js".

That'll open it up in a separate window, which you can kill whenever and just hit Up arrow on the original cmd window to run the same command.

On the off chance, you're killing the original cmd just to restart node, ctrl-c a couple times should kill it, shouldn't it?

Then up arrow and you've got the last command again.

This could use a little more context, but I assume you are running something like this on the command line:

node my_file.js

Or just:

./my_file.js

Whenever you write command line scripts like this, it's good to call process.exit to tell Node when you're done. So, for example:

#!/usr/bin/env node

function myFunction () {
  // Do some stuff that takes a while...
  return Promise.resolve()
}

myFunction()
  .then(result => {
    console.log(result)
    process.exit(0)
  })
  .catch(error => {
    console.error(error)
    process.exit(1)
  })

And if your script is just hanging and not stopping, you can always hit Ctrl + C to abort.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM