市场夺宝奇兵
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.

104 lines
2.6 KiB

  1. # npm-run-path
  2. > Get your [PATH](https://en.wikipedia.org/wiki/PATH_(variable)) prepended with locally installed binaries
  3. In [npm run scripts](https://docs.npmjs.com/cli/run-script) you can execute locally installed binaries by name. This enables the same outside npm.
  4. ## Install
  5. ```sh
  6. npm install npm-run-path
  7. ```
  8. ## Usage
  9. ```js
  10. import childProcess from 'node:child_process';
  11. import {npmRunPath, npmRunPathEnv} from 'npm-run-path';
  12. console.log(process.env.PATH);
  13. //=> '/usr/local/bin'
  14. console.log(npmRunPath());
  15. //=> '/Users/sindresorhus/dev/foo/node_modules/.bin:/Users/sindresorhus/dev/node_modules/.bin:/Users/sindresorhus/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/usr/local/bin'
  16. // `foo` is a locally installed binary
  17. childProcess.execFileSync('foo', {
  18. env: npmRunPathEnv()
  19. });
  20. ```
  21. ## API
  22. ### npmRunPath(options?)
  23. `options`: [`Options`](#options)\
  24. _Returns_: `string`
  25. Returns the augmented PATH string.
  26. ### npmRunPathEnv(options?)
  27. `options`: [`Options`](#options)\
  28. _Returns_: `object`
  29. Returns the augmented [`process.env`](https://nodejs.org/api/process.html#process_process_env) object.
  30. ### options
  31. Type: `object`
  32. #### cwd
  33. Type: `string | URL`\
  34. Default: `process.cwd()`
  35. The working directory.
  36. #### execPath
  37. Type: `string | URL`\
  38. Default: [`process.execPath`](https://nodejs.org/api/process.html#processexecpath)
  39. The path to the current Node.js executable.
  40. This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
  41. #### addExecPath
  42. Type: `boolean`\
  43. Default: `true`
  44. Whether to push the current Node.js executable's directory ([`execPath`](#execpath) option) to the front of PATH.
  45. #### preferLocal
  46. Type: `boolean`\
  47. Default: `true`
  48. Whether to push the locally installed binaries' directory to the front of PATH.
  49. #### path
  50. Type: `string`\
  51. Default: [`PATH`](https://github.com/sindresorhus/path-key)
  52. The PATH to be appended.
  53. Set it to an empty string to exclude the default PATH.
  54. Only available with [`npmRunPath()`](#npmrunpathoptions), not [`npmRunPathEnv()`](#npmrunpathenvoptions).
  55. #### env
  56. Type: `object`\
  57. Default: [`process.env`](https://nodejs.org/api/process.html#processenv)
  58. Accepts an object of environment variables, like `process.env`, and modifies the PATH using the correct [PATH key](https://github.com/sindresorhus/path-key). Use this if you're modifying the PATH for use in the `child_process` options.
  59. Only available with [`npmRunPathEnv()`](#npmrunpathenvoptions), not [`npmRunPath()`](#npmrunpathoptions).
  60. ## Related
  61. - [npm-run-path-cli](https://github.com/sindresorhus/npm-run-path-cli) - CLI for this module
  62. - [execa](https://github.com/sindresorhus/execa) - Execute a locally installed binary