win-path
Manipulate a Windows machine's PATH from Node.js.
Installation
npm install win-path
Quickstart
const path = // get the machine's PATH variablepath // => C:\rainbow;C:\unicorn const dir = 'C:\\kittens' // check whether dir is already in PATHpath // => false // add dir to the PATHpath // => C:\rainbow;C:\unicorn;C:\kittens // remove dir from the PATHpath // => C:\rainbow;C:\unicorn
API
-
dir
should be a non-empty string with thepath.sep
being\\
. Thedir
parameter is optional; the default value isprocess.cwd()
. -
target
must be one of['process', 'machine', 'user']
. If it is not, the returned Promise will reject withINVALID_TARGET
. Thetarget
parameter is optional; the default value isprocess
.
path.get([target])
- Resolves with the machine's current PATH variable.
path.has([dir, target])
- Resolves with
true
orfalse
depending on whether the specified directory is in the PATH or not.
path.add([dir, target])
- Adds the specified directory to the PATH variable and returns the new PATH.
- Resolves with
WARN_DIR_ALREADY_IN_PATH
if the directory is already in the PATH. - Resolves with
ERR_SECURITY_EXCEPTION
if adding the directory to the PATH failed due to security reasons (e.g. non-elevated process).
path.remove([dir, target])
- Removes the specified directory from the PATH variable and returns the new PATH.
- Resolves with
WARN_DIR_NOT_IN_PATH
if the directory is not present in the PATH. - Resolves with
ERR_SECURITY_EXCEPTION
if removing the directory from the PATH failed due to security reasons (e.g. non-elevated process).
Notes
- This module is based on the Powershell scripts in the
/scripts
directory. Therefore, to be able to use it in the first place, Powershell must be in the PATH (which it is by default on a new Windows machine). - Internally, the Powershell scripts use
[System.Environment]::GetEnvironmentVariable('path', target)
.
License
WTFPL – Do What the F*ck You Want to Public License.
Made with ❤️ by @MarkTiedemann.