78

Is yarn run intended to be the equivalent of npm start?

janw
  • 8,758
  • 11
  • 40
  • 62
daniely
  • 7,313
  • 5
  • 29
  • 46

4 Answers4

88

It seems yarn run start is the equivalent of npm start, which runs the script inside the start field of the script field in package.json

daniely
  • 7,313
  • 5
  • 29
  • 46
66

Few things to understand:

npm: run command is mandatory to execute user defined scripts.
yarn: run command is not mandatory to execute user defined scripts.

start command is not a user defined script name, so you may not need to specify run command to execute it.

So, all the below commands work similar!

  • npm start
  • npm run start
  • yarn start
  • yarn run start

If you have a user defined script named 'app':

  • npm app (Does not work!)
  • npm run app (Works!)
  • yarn app (Works!)
  • yarn run app (Works!)

Note: By default start runs node server.js in case not explicitly defined.

anothernode
  • 5,100
  • 13
  • 43
  • 62
19

npm start is a shortcut for npm run start

Now in terms of running scripts from package.json, all these are equivalent:

npm run start
npm start
yarn run start
yarn start

npm run myscript
npm myscript this is an error
yarn run myscript
yarn myscript

This is because run is not mandatory command for yarn, but it is for npm.


Bonus

npr start - OK
npr myscript - OK

Put this file somewhere in PATH, eg. %localappdata%\Programs\Git\cmd

npr.cmd
npm run %*
Qwerty
  • 29,062
  • 22
  • 108
  • 136
0

yarn run is similar to npm run, they can be used to run the scripts in package.json.

For npm, you can leave out run when run the npm lifecycle scripts (test, start, restart, and stop), but these scripts maybe have extra effect. For example, npm start will run node server.js if the "scripts" object does not define a "start" property in package.json. see the doc npm run. You can't run script with other name leaving out run.

For yarn, you can leave out run for all the scripts in package.json, but if your script name is same as yarn built-in cli commands, the built-in cli commands will have preference over your scripts. doc yarn run.

So the best way to run scripts in package.json: never leave out run.

Dreamoon
  • 369
  • 5
  • 8