Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling one script from another with Yarn

Below is an example how to run a script from another with npm in package.json.

What's the equivalent with yarn?

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "npm run clean",

  }
}
like image 216
Fellow Stranger Avatar asked Nov 27 '19 09:11

Fellow Stranger


People also ask

How do I run two scripts in npm?

Approach 1(npm-run all package): We can use the” npm-run all” package to run different scripts at the same time. First, we have to install the package itself by using the command. After installation of the package we have to navigate to the package.

What is yarn Postinstall?

postinstall is called after a package's dependency tree changes are written to the disk -- e.g. after a dependency or transitive dependency is added, removed, or changed. It is guaranteed to be called in topological order (in other words, your dependencies' postinstalls will always run before yours).

What does running yarn command do?

Running yarn <command> [<args>] will run the command, if it is matching a locally installed CLI. So you don't need to setup user-defined scripts for simple use cases.

Can npm be used with yarn?

Yarn can consume the same package. json format as npm, and can install any package from the npm registry. This will lay out your node_modules folder using Yarn's resolution algorithm that is compatible with the node.


2 Answers

You could do

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "yarn run clean",

  }
}

And then the command yarn run prebuildshould work. Also you could just do yarn run clean.

Documentation of the run cli : https://yarnpkg.com/lang/en/docs/cli/run/

like image 182
L. Faros Avatar answered Oct 09 '22 08:10

L. Faros


Improving on @l-faros answer, Yarn also supports a shorter syntax

{
 "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "yarn clean",

  }
}

and yarn prebuild to run the script command.

The syntax allows one to omit the run argument. Documentation: https://classic.yarnpkg.com/en/docs/cli/#toc-user-defined-scripts

like image 28
Murage Avatar answered Oct 09 '22 07:10

Murage