Skip to content

craft run ​

Run a package.json script, or list them when no name is given.

text
craft run [OPTIONS] [SCRIPT] [ARGS]...

Takes the global options.

Scripts and bins ​

craft run <name> runs, in order of preference:

  1. the script <name> in package.json;
  2. otherwise, the bin <name> from the nearest node_modules/.bin, searching the current directory and its parents.

Otherwise it fails, listing the scripts there are:

text
error: script "deploy" not found; available scripts: build, test

craft <name>, with any name that isn't a craft command, does the same: craft build, craft tsc --noEmit. To run only a bin, never a script, use craft exec.

Pre and post scripts ​

craft run build runs prebuild, build and postbuild, the ones that exist, stopping at the first that fails. Only build receives the arguments.

Arguments ​

Everything after the script name goes to the script, quoted so it reaches it as written, with no shell expansion:

sh
craft run test --watch
craft run greet "Ada Lovelace"      # the script gets one argument: Ada Lovelace
craft run show 'a; echo hacked'      # one argument; the echo never runs

Environment ​

Scripts run in a shell (sh -c on macOS and Linux, cmd /d /s /c on Windows) in the project directory, with:

VariableValue
PATHnode_modules/.bin of the project and every parent directory, then your PATH
npm_lifecycle_eventThe script's name, e.g. prebuild
npm_lifecycle_scriptThe script's command
npm_package_name, npm_package_versionFrom package.json
npm_package_jsonThe path of package.json
npm_config_user_agentcraft/<version> <os> <arch>, so tools such as create-vite know craft ran them
INIT_CWDThe directory craft was run from

Exit codes and signals ​

When a script fails, craft stops and exits with the script's exit code (128 + the signal number if it was killed), after one line:

text
error: build exited with code 2

Ctrl-C reaches the script, and craft waits for it to exit.

Listing scripts ​

text
$ craft run
Scripts in my-app:
  build
    tsc
  start
    node dist/server.js

Shortcuts ​

CommandSame as
craft startcraft run start
craft test, craft tcraft run test

Released under the MIT License.