Cli: [BUG] `npm -C ./dir exec cmd` fails

Created on 29 Oct 2020  路  8Comments  路  Source: npm/cli

Current Behavior:

npm -C ./dir exec foo fails with ENOENT, even if ./dir contains package.json.

Expected Behavior:

npm -C ./dir exec foo runs foo command in ./dir directory.

Steps To Reproduce:

$ cd $(mktemp -d)
$ mkdir dir
$ cd dir
$ npm i cowsay
$ npm exec cowsay a             # <- Success
$ cd ..
$ npm -C ./dir ls               # <- Success
$ npm -C ./dir exec cowsay a    # <- ERROR
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /tmp/tmp.PSJSuTrxHx/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/tmp/tmp.PSJSuTrxHx/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent

npm ERR! A complete log of this run can be found in:
npm ERR!     /tmp/tmp.vynlwg6kj4/.npm/_logs/2020-10-29T07_45_41_525Z-debug.log

Environment:

  • OS: Manjaro Linux(Linux acm-manjaro 4.19.147-1-MANJARO SMP Wed Sep 23 18:00:56 UTC 2020 x86_64 GNU/Linux)
  • Node: 15.0.1
  • npm: 7.0.6
Bug Release 7.x

Most helpful comment

ahhh TIL that too, sorry for misunderstanding.

All 8 comments

Try -c instead of -C.

@ljharb It also produces an error.

$ npm -c ./dir exec cowsay a
npm ERR! Run a command from a local or remote npm package.
npm ERR!
npm ERR! npm exec -- <pkg>[@<version>] [args...]
npm ERR! npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
npm ERR! npm exec -c '<cmd> [args...]'
npm ERR! npm exec --package=foo -c '<cmd> [args...]'
npm ERR!
npm ERR! npx <pkg>[@<specifier>] [args...]
npm ERR! npx -p <pkg>[@<specifier>] <cmd> [args...]
npm ERR! npx -c '<cmd> [args...]'
npm ERR! npx -p <pkg>[@<specifier>] -c '<cmd> [args...]'
npm ERR!
npm ERR! alias: x
npm ERR! common options:
npm ERR! --package=<pkg> (may be specified multiple times)
npm ERR! -p is a shorthand for --package only when using npx executable
npm ERR! -c <cmd> --call=<cmd> (may not be mixed with positional arguments)

And -c option is not listed as global options.

https://docs.npmjs.com/cli/v6/using-npm/config#shorthands-and-other-cli-niceties

I mean, --prefix option doesn't work with exec subcommand.

npm exec --help lists -c and not -C, so -C isn't a thing.

As for -c not working, I suspect #2081 might fix this?

@ljharb npm help ls doesn't list -C but npm -C ./dir ls works because it's global option(reference the link in my previous comment).

I think -C option should work with (almost) all subcommands.

TIL -C is a shorthand for --prefix 馃槄 https://github.com/npm/cli/blob/latest/lib/utils/config.js#L341

So yeah, given that lib/exec.js relies on prefix I'm quite sure #2081 will fix this one too. We should have a release today with it so that you can give it a try @acomagu.

ahhh TIL that too, sorry for misunderstanding.

Oh, thank you! I'm glad to know it.
I'm sorry for my poor explanation :pray:

Published in v7.0.7 馃帀 Thanks for the report @acomagu

Was this page helpful?
0 / 5 - 0 ratings