Yarn: How to pass input while running yarn run <script>

Created on 1 Nov 2016  路  10Comments  路  Source: yarnpkg/yarn

Node version: 5.6.0
Yarn version: v0.16.1
OS: OSX sierra

In npm it was possible to run a script by passing some input. Something like this:
npm run webpack-dev-server --testOffering=ngrl
and accessing it like process.env.npm_config_testOffering

What should be done to use similar setup while doing ?
yarn run webpack-dev-server

Most helpful comment

You can pass additional args using the following syntax.

yarn run <command> [-- <args>]

If your scripts is like this:

"scripts": {
  "dev": "webpack-dev-server"
}

You can run yarn run dev -- --port 9999, which will result on the following command being executed. webpack-dev-server --port 9999

All 10 comments

You can pass additional args using the following syntax.

yarn run <command> [-- <args>]

If your scripts is like this:

"scripts": {
  "dev": "webpack-dev-server"
}

You can run yarn run dev -- --port 9999, which will result on the following command being executed. webpack-dev-server --port 9999

How do I access the argument that I passed in a config file ?

I was able to access earlier in the config file using process.env.npm_config_

How do I do that now

I used to do this earlier
npm run webpack-prod --offering=ngrl

and the script looked like below:

"scripts": {
"webpack-prod": "webpack --colors --config webpack.config.prod.js"
}

should I parse process.argv ?

process.env.npm_config_ is set by NPM, something that YARN doesn't do. If you need custom arguments inside your script, you need to check webpackdocs.

According to https://github.com/webpack/webpack/issues/540, process.argv seems to be the best workaround until webpack 2 is landed (https://github.com/webpack/webpack/issues/2120), where you'll be able to set custom arguments using -env.

Yeah I guessed that. Thank you anyway.

Is this something that would be considered for future releases? I.e., can I file an issue? Or is it intentionally not implemented?

For some of my projects I use shell scripts and need access to the CLI variable somewhere in the middle of the command. E.g.,

"scripts": {
  "test": "browserify tests/$npm_config_component.js | test_runner"
}

And then run it with,

npm run test --component=widget
=> runs "browserify tests/widget.js | test_runner"

Looks like it's already been requested in https://github.com/yarnpkg/yarn/issues/1632

As a workaround, you can run it like this.

npm_config_component=widget yarn run test

Does the method above work even when there are multiple calls ? I want the same arg applied to BOTH calls webpack-dev-server and someothercall

Eg.

"scripts": {
  "dev": "webpack-dev-server && someothercall"
}

Note that this behavior has now changed:
warning From Yarn 1.0 onwards, scripts don't require "--" for options to be forwarded. In a future version, any explicit "--" will be forwarded as-is to the scripts.

So the prior example becomes:
yarn run dev --port 9999

Was this page helpful?
0 / 5 - 0 ratings

Related issues

torifat picture torifat  路  3Comments

MunifTanjim picture MunifTanjim  路  3Comments

ocolot picture ocolot  路  3Comments

danez picture danez  路  3Comments

AzGoalie picture AzGoalie  路  3Comments