You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # cross-spawn
  2. [![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Build status][appveyor-image]][appveyor-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]
  3. [npm-url]:https://npmjs.org/package/cross-spawn
  4. [downloads-image]:http://img.shields.io/npm/dm/cross-spawn.svg
  5. [npm-image]:http://img.shields.io/npm/v/cross-spawn.svg
  6. [travis-url]:https://travis-ci.org/IndigoUnited/node-cross-spawn
  7. [travis-image]:http://img.shields.io/travis/IndigoUnited/node-cross-spawn/master.svg
  8. [appveyor-url]:https://ci.appveyor.com/project/satazor/node-cross-spawn
  9. [appveyor-image]:https://img.shields.io/appveyor/ci/satazor/node-cross-spawn/master.svg
  10. [david-dm-url]:https://david-dm.org/IndigoUnited/node-cross-spawn
  11. [david-dm-image]:https://img.shields.io/david/IndigoUnited/node-cross-spawn.svg
  12. [david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-cross-spawn#info=devDependencies
  13. [david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-cross-spawn.svg
  14. A cross platform solution to node's spawn and spawnSync.
  15. ## Installation
  16. `$ npm install cross-spawn`
  17. If you are using `spawnSync` on node 0.10 or older, you will also need to install `spawn-sync`:
  18. `$ npm install spawn-sync`
  19. ## Why
  20. Node has issues when using spawn on Windows:
  21. - It ignores [PATHEXT](https://github.com/joyent/node/issues/2318)
  22. - It does not support [shebangs](http://pt.wikipedia.org/wiki/Shebang)
  23. - No `options.shell` support on node < v6
  24. - It does not allow you to run `del` or `dir`
  25. All these issues are handled correctly by `cross-spawn`.
  26. There are some known modules, such as [win-spawn](https://github.com/ForbesLindesay/win-spawn), that try to solve this but they are either broken or provide faulty escaping of shell arguments.
  27. ## Usage
  28. Exactly the same way as node's [`spawn`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options) or [`spawnSync`](https://nodejs.org/api/child_process.html#child_process_child_process_spawnsync_command_args_options), so it's a drop in replacement.
  29. ```js
  30. var spawn = require('cross-spawn');
  31. // Spawn NPM asynchronously
  32. var child = spawn('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });
  33. // Spawn NPM synchronously
  34. var results = spawn.sync('npm', ['list', '-g', '-depth', '0'], { stdio: 'inherit' });
  35. ```
  36. ## Caveats
  37. #### `options.shell` as an alternative to `cross-spawn`
  38. Starting from node v6, `spawn` has a `shell` option that allows you run commands from within a shell. This new option solves most of the problems that `cross-spawn` attempts to solve, but:
  39. - It's not supported in node < v6
  40. - It has no support for shebangs on Windows
  41. - You must manually escape the command and arguments which is very error prone, specially when passing user input
  42. If you are using the `shell` option to spawn a command in a cross platform way, consider using `cross-spawn` instead. You have been warned.
  43. #### Shebangs
  44. While `cross-spawn` handles shebangs on Windows, its support is limited: e.g.: it doesn't handle arguments after the path, e.g.: `#!/bin/bash -e`.
  45. Remember to always test your code on Windows!
  46. ## Tests
  47. `$ npm test`
  48. ## License
  49. Released under the [MIT License](http://www.opensource.org/licenses/mit-license.php).