Skip to content
This repository has been archived by the owner on Jan 6, 2021. It is now read-only.

cross-env is swallowing "Ctrl + C" command on Windows 10 #178

Closed
bennycode opened this issue May 14, 2018 · 5 comments
Closed

cross-env is swallowing "Ctrl + C" command on Windows 10 #178

bennycode opened this issue May 14, 2018 · 5 comments

Comments

@bennycode
Copy link
Contributor

  • cross-env version: 5.1.5
  • node version: 8.9.4
  • yarn version: 1.3.2

Relevant code or config:

package.json

"start": "cross-env NODE_ENV=production node dist/app/monitor.js",

monitor.ts

import {Monitor} from 'forever-monitor';
import * as path from 'path';

let child: Monitor | undefined = undefined;

function exit(signal: string) {
  console.warn(`HERE I AM!!`);
  if (child) child.stop();
  process.exit(0);
}

process.on('SIGINT', exit);

child = new Monitor(path.join(__dirname, 'index.js'));

child.start();

What you did:

  1. I started my transpiled "monitor.js" file using node dist/app/monitor.js
  2. I hit Ctrl + C (on Windows 10) while the program was running
  3. I saw the "HERE I AM!!" log message, because my code received a SIGINT
  4. I tried the same again but this time with cross-env using npx cross-env NODE_ENV=production node dist/app/monitor.js
  5. I hit Ctrl + C
  6. I couldn't see the "HERE I AM!!" log message because it looks like cross-env is eating up my Ctrl + C input

Expected result:

I was thinking that my code will run into the exit function when running my program with cross-env using npx cross-env NODE_ENV=production node dist/app/monitor.js and pressing Ctrl + C but unfortunately it doesn't.

My process.on('SIGINT') handler is only called when running node dist/app/monitor.js (without "cross-env").

@kentcdodds
Copy link
Owner

Thanks for the report @bennyn. Would you be willing to dig a little deeper and suggest a solution for this problem?

@bennycode
Copy link
Contributor Author

I will try to dig a little deeper but first I need to understand how signal interrupts are passed on in Node.js.

I saw that @slavik57 reported something similar (CTRL+C does not kill the child process) and traced it down to Node itself. 🤔

@bennycode
Copy link
Contributor Author

bennycode commented May 15, 2018

Great insights! I found out that if I replace cross-env with cross-env-shell, then my program receives the 'SIGINT' on Windows.

I then followed your best practices and split my start script into environment variable assignment and execution:

"start": "yarn start:monitor",
"start:monitor": "cross-env NODE_ENV=production yarn start:monitor:shell"
"start:monitor:shell":" "cross-env-shell node dist/app/monitor.js",

This works now for me so I will close the isse! 🎉 But it might be considered mentioning this within the README.md file. What do you think?

@kentcdodds
Copy link
Owner

I'm glad you got that worked out! Feel free to open a PR to update the README.

@bennycode
Copy link
Contributor Author

Your wish is my command! 😃
#179

@baerrach baerrach mentioned this issue Feb 13, 2020
3 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants