DEV Community

loading...

Prefer npm scripts to global commands

Evgeny Orekhov
・1 min read

Define npm scripts in your package.json:

"start": "ember serve",
"test": "ember exam --load-balance --parallel=4",
"lint": "npm-run-all --parallel --aggregate-output lint:**",
Enter fullscreen mode Exit fullscreen mode

and use those scripts, don't use global commands.

Bad: ember test (yes, it's bad even if you see it in the official docs)

Good: npm test

There are several benefits to using npm scripts instead of global commands:

  1. No chance of local vs global package version mismatch that can lead to inexplicable errors
  2. No chance of using the wrong command and/or options (for example, the default command for running tests in Ember projects is ember test, but a project might use ember exam and/or additional options like --load-balance --parallel=4)
  3. No need to remember specific commands for each type of project, because npm start and npm test are standard commands used by virtually every JavaScript project, be it an Ember project, a React project, or an Express project
  4. No need to install global packages, keep track of their versions, and update them

If you really need to run a one-off command, use npx, it will protect you from local vs global package version mismatch (because it uses local package if it detects one):

npx prettier --write .
Enter fullscreen mode Exit fullscreen mode

Discussion (0)