DEV Community

Evgeny Orekhov
Evgeny Orekhov

Posted on

Prefer npm scripts to global commands

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay