1 | <div align="center">
|
2 | <h1>cross-env π</h1>
|
3 |
|
4 | <p>Run scripts that set and use environment variables across platforms</p>
|
5 | </div>
|
6 |
|
7 | **π¨ NOTICE: cross-env still works well, but is in maintenance mode. No new
|
8 | features will be added, only serious and common-case bugs will be fixed, and
|
9 | it will only be kept up-to-date with Node.js over time.
|
10 | [Learn more](https://github.com/kentcdodds/cross-env/issues/257)**
|
11 |
|
12 | ---
|
13 |
|
14 |
|
15 | [![Build Status][build-badge]][build]
|
16 | [![Code Coverage][coverage-badge]][coverage]
|
17 | [![version][version-badge]][package]
|
18 | [![downloads][downloads-badge]][npmtrends]
|
19 | [![MIT License][license-badge]][license]
|
20 | [![All Contributors][all-contributors-badge]](#contributors-)
|
21 | [![PRs Welcome][prs-badge]][prs]
|
22 | [![Code of Conduct][coc-badge]][coc]
|
23 |
|
24 |
|
25 | ## The problem
|
26 |
|
27 | Most Windows command prompts will choke when you set environment variables with
|
28 | `NODE_ENV=production` like that. (The exception is [Bash on Windows][win-bash],
|
29 | which uses native Bash.) Similarly, there's a difference in how windows and
|
30 | POSIX commands utilize environment variables. With POSIX, you use: `$ENV_VAR`
|
31 | and on windows you use `%ENV_VAR%`.
|
32 |
|
33 | ## This solution
|
34 |
|
35 | `cross-env` makes it so you can have a single command without worrying about
|
36 | setting or using the environment variable properly for the platform. Just set it
|
37 | like you would if it's running on a POSIX system, and `cross-env` will take care
|
38 | of setting it properly.
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | - [Installation](#installation)
|
44 | - [Usage](#usage)
|
45 | - [`cross-env` vs `cross-env-shell`](#cross-env-vs-cross-env-shell)
|
46 | - [Windows Issues](#windows-issues)
|
47 | - [Inspiration](#inspiration)
|
48 | - [Other Solutions](#other-solutions)
|
49 | - [Contributors](#contributors)
|
50 | - [LICENSE](#license)
|
51 |
|
52 |
|
53 |
|
54 | ## Installation
|
55 |
|
56 | This module is distributed via [npm][npm] which is bundled with [node][node] and
|
57 | should be installed as one of your project's `devDependencies`:
|
58 |
|
59 | ```
|
60 | npm install --save-dev cross-env
|
61 | ```
|
62 |
|
63 | > WARNING! Make sure that when you're installing packages that you spell things
|
64 | > correctly to avoid [mistakenly installing malware][malware]
|
65 |
|
66 | > NOTE : Version 7 of cross-env only supports Node.js 10 and higher, to use it on
|
67 | > Node.js 8 or lower install version 6 `npm install --save-dev cross-env@6`
|
68 |
|
69 | ## Usage
|
70 |
|
71 | I use this in my npm scripts:
|
72 |
|
73 | ```json
|
74 | {
|
75 | "scripts": {
|
76 | "build": "cross-env NODE_ENV=production webpack --config build/webpack.config.js"
|
77 | }
|
78 | }
|
79 | ```
|
80 |
|
81 | Ultimately, the command that is executed (using [`cross-spawn`][cross-spawn])
|
82 | is:
|
83 |
|
84 | ```
|
85 | webpack --config build/webpack.config.js
|
86 | ```
|
87 |
|
88 | The `NODE_ENV` environment variable will be set by `cross-env`
|
89 |
|
90 | You can set multiple environment variables at a time:
|
91 |
|
92 | ```json
|
93 | {
|
94 | "scripts": {
|
95 | "build": "cross-env FIRST_ENV=one SECOND_ENV=two node ./my-program"
|
96 | }
|
97 | }
|
98 | ```
|
99 |
|
100 | You can also split a command into several ones, or separate the environment
|
101 | variables declaration from the actual command execution. You can do it this way:
|
102 |
|
103 | ```json
|
104 | {
|
105 | "scripts": {
|
106 | "parentScript": "cross-env GREET=\"Joe\" npm run childScript",
|
107 | "childScript": "cross-env-shell \"echo Hello $GREET\""
|
108 | }
|
109 | }
|
110 | ```
|
111 |
|
112 | Where `childScript` holds the actual command to execute and `parentScript` sets
|
113 | the environment variables to use. Then instead of run the childScript you run
|
114 | the parent. This is quite useful for launching the same command with different
|
115 | env variables or when the environment variables are too long to have everything
|
116 | in one line. It also means that you can use `$GREET` env var syntax even on
|
117 | Windows which would usually require it to be `%GREET%`.
|
118 |
|
119 | If you precede a dollar sign with an odd number of backslashes the expression
|
120 | statement will not be replaced. Note that this means backslashes after the JSON
|
121 | string escaping took place. `"FOO=\\$BAR"` will not be replaced.
|
122 | `"FOO=\\\\$BAR"` will be replaced though.
|
123 |
|
124 | Lastly, if you want to pass a JSON string (e.g., when using [ts-loader]), you
|
125 | can do as follows:
|
126 |
|
127 | ```json
|
128 | {
|
129 | "scripts": {
|
130 | "test": "cross-env TS_NODE_COMPILER_OPTIONS={\\\"module\\\":\\\"commonjs\\\"} node some_file.test.ts"
|
131 | }
|
132 | }
|
133 | ```
|
134 |
|
135 | Pay special attention to the **triple backslash** `(\\\)` **before** the
|
136 | **double quotes** `(")` and the **absence** of **single quotes** `(')`. Both of
|
137 | these conditions have to be met in order to work both on Windows and UNIX.
|
138 |
|
139 | ## `cross-env` vs `cross-env-shell`
|
140 |
|
141 | The `cross-env` module exposes two bins: `cross-env` and `cross-env-shell`. The
|
142 | first one executes commands using [`cross-spawn`][cross-spawn], while the second
|
143 | one uses the `shell` option from Node's `spawn`.
|
144 |
|
145 | The main use case for `cross-env-shell` is when you need an environment variable
|
146 | to be set across an entire inline shell script, rather than just one command.
|
147 |
|
148 | For example, if you want to have the environment variable apply to several
|
149 | commands in series then you will need to wrap those in quotes and use
|
150 | `cross-env-shell` instead of `cross-env`.
|
151 |
|
152 | ```json
|
153 | {
|
154 | "scripts": {
|
155 | "greet": "cross-env-shell GREETING=Hi NAME=Joe \"echo $GREETING && echo $NAME\""
|
156 | }
|
157 | }
|
158 | ```
|
159 |
|
160 | The rule of thumb is: if you want to pass to `cross-env` a command that contains
|
161 | special shell characters _that you want interpreted_, then use
|
162 | `cross-env-shell`. Otherwise stick to `cross-env`.
|
163 |
|
164 | On Windows you need to use `cross-env-shell`, if you want to handle
|
165 | [signal events](https://nodejs.org/api/process.html#process_signal_events)
|
166 | inside of your program. A common case for that is when you want to capture a
|
167 | `SIGINT` event invoked by pressing `Ctrl + C` on the command-line interface.
|
168 |
|
169 | ## Windows Issues
|
170 |
|
171 | Please note that `npm` uses `cmd` by default and that doesn't support command
|
172 | substitution, so if you want to leverage that, then you need to update your
|
173 | `.npmrc` to set the `script-shell` to powershell.
|
174 | [Learn more here](https://github.com/kentcdodds/cross-env/issues/192#issuecomment-513341729).
|
175 |
|
176 | ## Inspiration
|
177 |
|
178 | I originally created this to solve a problem I was having with my npm scripts in
|
179 | [angular-formly][angular-formly]. This made contributing to the project much
|
180 | easier for Windows users.
|
181 |
|
182 | ## Other Solutions
|
183 |
|
184 | - [`env-cmd`](https://github.com/toddbluhm/env-cmd) - Reads environment
|
185 | variables from a file instead
|
186 | - [`@naholyr/cross-env`](https://www.npmjs.com/package/@naholyr/cross-env) -
|
187 | `cross-env` with support for setting default values
|
188 |
|
189 | ## Issues
|
190 |
|
191 | _Looking to contribute? Look for the [Good First Issue][good-first-issue]
|
192 | label._
|
193 |
|
194 | ### π Bugs
|
195 |
|
196 | Please file an issue for bugs, missing documentation, or unexpected behavior.
|
197 |
|
198 | [**See Bugs**][bugs]
|
199 |
|
200 | ### π‘ Feature Requests
|
201 |
|
202 | This project is in maintenance mode and no new feature requests will be considered.
|
203 |
|
204 | [**Learn more**](https://github.com/kentcdodds/cross-env/issues/257)
|
205 |
|
206 | ## Contributors β¨
|
207 |
|
208 | Thanks goes to these people ([emoji key][emojis]):
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | <table>
|
214 | <tr>
|
215 | <td align="center"><a href="https://kentcdodds.com"><img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;" alt=""/><br /><sub><b>Kent C. Dodds</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=kentcdodds" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=kentcdodds" title="Documentation">π</a> <a href="#infra-kentcdodds" title="Infrastructure (Hosting, Build-Tools, etc)">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=kentcdodds" title="Tests">β οΈ</a></td>
|
216 | <td align="center"><a href="https://zhuangya.me"><img src="https://avatars1.githubusercontent.com/u/499038?v=3" width="100px;" alt=""/><br /><sub><b>Ya Zhuang </b></sub></a><br /><a href="#plugin-zhuangya" title="Plugin/utility libraries">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=zhuangya" title="Documentation">π</a></td>
|
217 | <td align="center"><a href="https://wopian.me"><img src="https://avatars3.githubusercontent.com/u/3440094?v=3" width="100px;" alt=""/><br /><sub><b>James Harris</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=wopian" title="Documentation">π</a></td>
|
218 | <td align="center"><a href="https://github.com/compumike08"><img src="https://avatars1.githubusercontent.com/u/8941730?v=3" width="100px;" alt=""/><br /><sub><b>compumike08</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Acompumike08" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=compumike08" title="Documentation">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=compumike08" title="Tests">β οΈ</a></td>
|
219 | <td align="center"><a href="https://github.com/danielo515"><img src="https://avatars1.githubusercontent.com/u/2270425?v=3" width="100px;" alt=""/><br /><sub><b>Daniel RodrΓguez Rivero</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Adanielo515" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=danielo515" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=danielo515" title="Documentation">π</a></td>
|
220 | <td align="center"><a href="https://github.com/inyono"><img src="https://avatars2.githubusercontent.com/u/1508477?v=3" width="100px;" alt=""/><br /><sub><b>Jonas Keinholz</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Ainyono" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=inyono" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=inyono" title="Tests">β οΈ</a></td>
|
221 | <td align="center"><a href="https://github.com/hgwood"><img src="https://avatars3.githubusercontent.com/u/1656170?v=3" width="100px;" alt=""/><br /><sub><b>Hugo Wood</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Ahgwood" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=hgwood" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=hgwood" title="Tests">β οΈ</a></td>
|
222 | </tr>
|
223 | <tr>
|
224 | <td align="center"><a href="https://github.com/thomasthiebaud"><img src="https://avatars0.githubusercontent.com/u/3715715?v=3" width="100px;" alt=""/><br /><sub><b>Thiebaud Thomas</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Athomasthiebaud" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=thomasthiebaud" title="Tests">β οΈ</a></td>
|
225 | <td align="center"><a href="https://daniel.blog"><img src="https://avatars1.githubusercontent.com/u/1715800?v=3" width="100px;" alt=""/><br /><sub><b>Daniel Rey LΓ³pez</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=DanReyLop" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=DanReyLop" title="Tests">β οΈ</a></td>
|
226 | <td align="center"><a href="http://amilajack.com"><img src="https://avatars2.githubusercontent.com/u/6374832?v=3" width="100px;" alt=""/><br /><sub><b>Amila Welihinda</b></sub></a><br /><a href="#infra-amilajack" title="Infrastructure (Hosting, Build-Tools, etc)">π</a></td>
|
227 | <td align="center"><a href="https://twitter.com/paulcbetts"><img src="https://avatars1.githubusercontent.com/u/1396?v=3" width="100px;" alt=""/><br /><sub><b>Paul Betts</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Apaulcbetts" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=paulcbetts" title="Code">π»</a></td>
|
228 | <td align="center"><a href="https://github.com/turnerhayes"><img src="https://avatars1.githubusercontent.com/u/6371670?v=3" width="100px;" alt=""/><br /><sub><b>Turner Hayes</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Aturnerhayes" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=turnerhayes" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=turnerhayes" title="Tests">β οΈ</a></td>
|
229 | <td align="center"><a href="https://github.com/sudo-suhas"><img src="https://avatars2.githubusercontent.com/u/22251956?v=4" width="100px;" alt=""/><br /><sub><b>Suhas Karanth</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=sudo-suhas" title="Tests">β οΈ</a></td>
|
230 | <td align="center"><a href="https://github.com/sventschui"><img src="https://avatars3.githubusercontent.com/u/512692?v=4" width="100px;" alt=""/><br /><sub><b>Sven</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=sventschui" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=sventschui" title="Documentation">π</a> <a href="#example-sventschui" title="Examples">π‘</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=sventschui" title="Tests">β οΈ</a></td>
|
231 | </tr>
|
232 | <tr>
|
233 | <td align="center"><a href="https://github.com/NicoZelaya"><img src="https://avatars0.githubusercontent.com/u/5522668?v=4" width="100px;" alt=""/><br /><sub><b>D. NicolΓ‘s Lopez Zelaya</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=NicoZelaya" title="Code">π»</a></td>
|
234 | <td align="center"><a href="http://bithavoc.io"><img src="https://avatars3.githubusercontent.com/u/219289?v=4" width="100px;" alt=""/><br /><sub><b>Johan Hernandez</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=bithavoc" title="Code">π»</a></td>
|
235 | <td align="center"><a href="https://github.com/jnielson94"><img src="https://avatars3.githubusercontent.com/u/13559161?v=4" width="100px;" alt=""/><br /><sub><b>Jordan Nielson</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/issues?q=author%3Ajnielson94" title="Bug reports">π</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=jnielson94" title="Code">π»</a> <a href="https://github.com/kentcdodds/cross-env/commits?author=jnielson94" title="Tests">β οΈ</a></td>
|
236 | <td align="center"><a href="https://nz.linkedin.com/in/jsonc11"><img src="https://avatars0.githubusercontent.com/u/5185660?v=4" width="100px;" alt=""/><br /><sub><b>Jason Cooke</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=Jason-Cooke" title="Documentation">π</a></td>
|
237 | <td align="center"><a href="https://github.com/bibo5088"><img src="https://avatars0.githubusercontent.com/u/17709887?v=4" width="100px;" alt=""/><br /><sub><b>bibo5088</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=bibo5088" title="Code">π»</a></td>
|
238 | <td align="center"><a href="https://codefund.io"><img src="https://avatars2.githubusercontent.com/u/12481?v=4" width="100px;" alt=""/><br /><sub><b>Eric Berry</b></sub></a><br /><a href="#fundingFinding-coderberry" title="Funding Finding">π</a></td>
|
239 | <td align="center"><a href="https://michaeldeboey.be"><img src="https://avatars3.githubusercontent.com/u/6643991?v=4" width="100px;" alt=""/><br /><sub><b>MichaΓ«l De Boey</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=MichaelDeBoey" title="Code">π»</a></td>
|
240 | </tr>
|
241 | <tr>
|
242 | <td align="center"><a href="https://github.com/lauriii"><img src="https://avatars0.githubusercontent.com/u/1845495?v=4" width="100px;" alt=""/><br /><sub><b>Lauri Eskola</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=lauriii" title="Documentation">π</a></td>
|
243 | <td align="center"><a href="https://github.com/devuxer"><img src="https://avatars0.githubusercontent.com/u/1298521?v=4" width="100px;" alt=""/><br /><sub><b>devuxer</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=devuxer" title="Documentation">π</a></td>
|
244 | <td align="center"><a href="https://github.com/dsbert"><img src="https://avatars2.githubusercontent.com/u/1320090?v=4" width="100px;" alt=""/><br /><sub><b>Daniel</b></sub></a><br /><a href="https://github.com/kentcdodds/cross-env/commits?author=dsbert" title="Documentation">π</a></td>
|
245 | </tr>
|
246 | </table>
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | This project follows the [all-contributors][all-contributors] specification.
|
253 | Contributions of any kind welcome!
|
254 |
|
255 | > Note: this was added late into the project. If you've contributed to this
|
256 | > project in any way, please make a pull request to add yourself to the list by
|
257 | > following the instructions in the `CONTRIBUTING.md`
|
258 |
|
259 | ## LICENSE
|
260 |
|
261 | MIT
|
262 |
|
263 |
|
264 | [npm]: https://npmjs.com
|
265 | [node]: https://nodejs.org
|
266 | [build-badge]: https://img.shields.io/github/workflow/status/kentcdodds/cross-env/validate?logo=github&style=flat-square
|
267 | [build]: https://github.com/kentcdodds/cross-env/actions?query=workflow%3Avalidate
|
268 | [coverage-badge]: https://img.shields.io/codecov/c/github/kentcdodds/cross-env.svg?style=flat-square
|
269 | [coverage]: https://codecov.io/github/kentcdodds/cross-env
|
270 | [version-badge]: https://img.shields.io/npm/v/gatsby-remark-embedder.svg?style=flat-square
|
271 | [package]: https://www.npmjs.com/package/gatsby-remark-embedder
|
272 | [downloads-badge]: https://img.shields.io/npm/dm/gatsby-remark-embedder.svg?style=flat-square
|
273 | [npmtrends]: http://www.npmtrends.com/gatsby-remark-embedder
|
274 | [license-badge]: https://img.shields.io/npm/l/gatsby-remark-embedder.svg?style=flat-square
|
275 | [license]: https://github.com/kentcdodds/cross-env/blob/master/LICENSE
|
276 | [prs-badge]: https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square
|
277 | [prs]: http://makeapullrequest.com
|
278 | [coc-badge]: https://img.shields.io/badge/code%20of-conduct-ff69b4.svg?style=flat-square
|
279 | [coc]: https://github.com/kentcdodds/cross-env/blob/master/other/CODE_OF_CONDUCT.md
|
280 | [emojis]: https://allcontributors.org/docs/en/emoji-key
|
281 | [all-contributors]: https://github.com/all-contributors/all-contributors
|
282 | [all-contributors-badge]: https://img.shields.io/github/all-contributors/kentcdodds/cross-env?color=orange&style=flat-square
|
283 | [bugs]: https://github.com/kentcdodds/cross-env/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3A%22%F0%9F%90%9B+Bug%22+sort%3Acreated-desc
|
284 | [good-first-issue]: https://github.com/kentcdodds/cross-env/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+sort%3Areactions-%2B1-desc+label%3A%22good+first+issue%22
|
285 |
|
286 | [angular-formly]: https://github.com/formly-js/angular-formly
|
287 | [cross-spawn]: https://www.npmjs.com/package/cross-spawn
|
288 | [malware]: http://blog.npmjs.org/post/163723642530/crossenv-malware-on-the-npm-registry
|
289 | [ts-loader]: https://www.npmjs.com/package/ts-loader
|
290 | [win-bash]: https://msdn.microsoft.com/en-us/commandline/wsl/about
|
291 |
|