Does `npx` provide a way to invoke a package script?

I’ve read:



But I’m not really clear on what the takeaway is.

I’m just looking to have npx download my module and run its start script.

I’ve tried a whole bunch of things with the -p and -c flags too but couldn’t come up with the right combination.

Thanks ahead of time :slight_smile:

Neglected to mention – here’s my start script:

"start": "ts-node server.ts"

[Edit: This comment was wrong, so struck-out.]

Those links look similar but I think are about a slightly different use case than yours, and happily your situation is supported.

You want `-p` and not `-c`, like: `npx -p yourPackage npm run start` which downloads the package, then calls a command with the package available.

Hmm, that doesn’t appear to have worked. It seems to be looking in my current directory for a package.json file for some reason.

$ npx -p git+https://my-package.git npm run start
npm ERR! path /fakepath/package.json
npm ERR! code ENOENT
npm ERR! errno -2
npm ERR! syscall open
npm ERR! enoent ENOENT: no such file or directory, open '/fakepath/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

Is there something I should be setting my bin to in my package.json?

Oops, I got it wrong about what the -p allows, sorry. (I quickly tested it but in a directory that happened to have a compatible package.json!)

Yes, you can use npx to run commands you declare in your package using bin.

But I shouldn’t have to specify a bin, right? Because I’m just trying to invoke a package script?

It looks like now it’s trying to run a binary the same name as the package:

$ npx git+https://my-package.git npm start
command not found: my-package

I was wrong about running scripts, I think you do need to specify a bin to do what you want.

Assuming package A includes a command (bin) B you can run:

npx -p A B

And if package A includes a command (bin) A as is often the case:

npx A

Do you know if there’s anything I can do to get this post some more attention?

In particular I’m interested in the workaround that @zcat proposed here: https://github.com/zkat/npx/issues/107#issuecomment-320644119, that seems like it worked for @ljharb.

Again, just to be clear: I’m looking for one command that downloads my module and runs its package script. If anyone has any suggestions for how to do this, or if using bin is my only option, please let me know.

npm run-scripts are only for developing the package they’re defined in; conceptually and semantically, you want a “bin” instead, which is what npx could invoke.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.