https://github.com/shinnn/prepend-path
Prepend a path to the existing PATH environment variable cross-platform way
https://github.com/shinnn/prepend-path
cross-platform environment-variables javascript nodejs path prepend
Last synced: 2 months ago
JSON representation
Prepend a path to the existing PATH environment variable cross-platform way
- Host: GitHub
- URL: https://github.com/shinnn/prepend-path
- Owner: shinnn
- License: isc
- Created: 2017-12-13T11:03:19.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-20T21:53:48.000Z (over 6 years ago)
- Last Synced: 2025-03-04T15:48:18.029Z (3 months ago)
- Topics: cross-platform, environment-variables, javascript, nodejs, path, prepend
- Language: JavaScript
- Size: 43 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# prepend-path
[](https://www.npmjs.com/package/prepend-path)
[](https://travis-ci.org/shinnn/prepend-path)
[](https://coveralls.io/github/shinnn/prepend-path?branch=master)Prepend a path to the existing [`PATH`](http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html#tag_08_03) environment variable cross-platform way
```javascript
const prependPath = require('prepend-path');process.env.PATH; //=> '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
prependPath('additional/bin');
process.env.PATH; //=> 'additional/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'
```## Installation
[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/getting-started/what-is-npm).
```
npm install prepend-path
```## API
```javascript
const prependPath = require('prepend-path');
```### prependPath(*path*)
*path*: `string` (a path to prepend)
Return: `string` (modified `PATH` environment variable)It prepends the given *path* to the [`process.env.PATH`](https://nodejs.org/api/process.html#process_process_env), or [its equivalent on Windows](https://stackoverflow.com/questions/7199039/file-paths-in-windows-environment-not-case-sensitive/7199074#7199074) for example `process.env.Path`, along with [the platform-specific path delimiter](https://nodejs.org/api/path.html#path_path_delimiter).
```javascript
prependPath('foo/bar');
// POSIX
//=> 'foo/bar:/usr/local/bin:/usr/bin:/bin:...'// Windows
//=> 'foo\\bar;C:\\Users\\appveyor\\AppData\\Roaming\\npm;C:\\...'
```Prepending a new path to the `PATH` is, in other words, making it precedent to the every existing one while searching executable files.
```javascript
const {existsSync} = require('fs');
const which = require('which');existsSync('/User/example/new_path/npm/bin/npm'); //=> true
which.sync('npm'); //=> '/usr/local/bin/npm'
prependPath('/User/example/new_path/npm/bin');
which.sync('npm'); //=> '/User/example/new_path/npm/bin/npm'
```## License
[ISC License](./LICENSE) © 2017 - 2018 Shinnosuke Watanabe