Evya
Evya

Reputation: 2385

Can I run a command from package.json bin script?

Is it possible to run a command from a bin script in a package.json ? I know it expects a path to file and trying to run a command there results in an error upon installation (after publishing to npm). Is it possible to run a command like it is in an npm start ?

Examples:

{
  "name": "myscript",
  "version": "1.0.3",
  "bin": {
    "myscript": "app/main.js"
  }
}

This will create a symlink from the app/main.js script to /usr/local/bin/myscript
Instead, this is what I want to achieve:

{
  "name": "myscript",
  "version": "1.0.3",
  "bin": {
    "myscript": "echo hello world"
  }
}

Possible workarounds are also appreciated.

Upvotes: 7

Views: 14771

Answers (2)

Wil Moore III
Wil Moore III

Reputation: 7214

This answer is updated since the old answer was a bit dated and ultimately incorrect. You can now do:

npx myscript

Upvotes: 6

Lingster
Lingster

Reputation: 1087

Have you tried running npm link https://docs.npmjs.com/cli/link which would create a link to your binary, then you can just run your script on the command line as myscript.

Upvotes: 2

Related Questions