Prepend a path to the existing PATH
environment variable cross-platform way
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'
npm install prepend-path
const prependPath = require('prepend-path');
path: string
(a path to prepend)
Return: string
(modified PATH
environment variable)
It prepends the given path to the process.env.PATH
, or its equivalent on Windows for example process.env.Path
, along with the platform-specific path delimiter.
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.
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'
ISC License © 2017 - 2018 Shinnosuke Watanabe