Skip to content

Prepend a path to the existing PATH environment variable cross-platform way

License

Notifications You must be signed in to change notification settings

shinnn/prepend-path

Repository files navigation

prepend-path

npm version Build Status Coverage Status

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'

Installation

Use npm.

npm install prepend-path

API

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, 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'

License

ISC License © 2017 - 2018 Shinnosuke Watanabe

About

Prepend a path to the existing PATH environment variable cross-platform way

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published