Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

package.main with -- arguments not working when running an npm script #1810

Closed
madarche opened this issue Dec 11, 2020 · 4 comments
Closed

Comments

@madarche
Copy link

madarche commented Dec 11, 2020

  • nodemon -v: 2.0.6
  • node -v: 12.18.2
  • Operating system/terminal environment: Debian GNU/Linux stable
  • Using Docker? What image: No
  • Command you ran: npm run start:watch -- -L -opt2 -opt3

Expected behaviour

[nodemon] starting `node src/main.js -L -opt2 -opt3

Actual behaviour

[nodemon] starting `node -opt2 -opt3 src/main.js`
node: bad option: -opt2
node: bad option: -opt3
[nodemon] app crashed - waiting for file changes before starting...

Steps to reproduce

package.json:

{
  "scripts": {
    "start": "node src/main.js",
    "start:watch": "nodemon"
  },
  "main": "src/main.js",
  "devDependencies": {
    "nodemon": "2.0.6"
  }
}
$ npm run start:watch -- -L -opt2 -opt3

> @ start:watch /home/joe/myapp
> nodemon "-L" "-opt2" "-opt3"

[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node -opt2 -opt3 src/main.js`
node: bad option: -opt2
node: bad option: -opt3
[nodemon] app crashed - waiting for file changes before starting...

While the following works fine:

$ node_modules/.bin/nodemon -- -L -opt2 -opt3
[nodemon] 2.0.6
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/main.js -L -opt2 -opt3`

This is related with https://github.com/remy/nodemon/blob/master/faq.md#my-script-arguments-are-being-taken-by-nodemon, #1773 and #1758

@remy
Copy link
Owner

remy commented Dec 11, 2020

Yeah… so, you're hitting some interesting combination of using nodemon and npm with the "stop eating" double dash thing.

When you run a program (any) with the -- argument, it takes everything after and passes it to the sub process.

So when you run the npm command npm run start:watch -- -L -opt2 -opt3 what nodemon sees is: nodemon -L -opt2 -opt3 - because npm has removed the -- from the arguments.

When nodemon runs with -L -opt2 -opt3 it recognises the -L as being for itself (i.e. it's a nodemon specific flag) and the unknown flags are treated as flags for the executing process, ie. node. Which is why you're seeing the weird starting mode: node -opt2 -opt3 src/main.js

There two work arounds for this:

  1. Kinda ugly, but this will work - add another --: npm run start:watch -- -- -L -opt2 -opt3 - I'm not keen, but it works without any changes to your code.
  2. A small tweak to your package, set "start:watch": "nodemon src/main.js" now doing npm run start:watch -- -L -opt2 -opt3, the -L goes to nodemon (for "legacy mode") and the options go to your script.

I might add this to the faq too actually.

Finally, thank you so much for your sponsorship and support - genuinely valued.

@remy remy closed this as completed Dec 11, 2020
@madarche
Copy link
Author

@remy as for the sponsorship, this need was the right opportunity to thank you, this was long overdue!

@madarche
Copy link
Author

To be complete, I should have added that I run successfully npm run start:watch -- -L -opt2 -opt3 on many projects, but those projects don't have a package.main.

@madarche
Copy link
Author

The solution of using "start:watch": "nodemon src/main.js" in package.json fits me fine because that way the developers can always use the same command npm run start:watch -- -L -opt2 -opt3 on all projects!

Thank you @remy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants