npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

windows-environment

v1.0.2

Published

A powerful library to manage Environment Variables on Windows using Node.JS

Downloads

13

Readme

Windows Environment Variable Management Library for JS Lovers

:rocket: :telescope: A powerful library to manage Environment Variables on Windows using Node.JS for Electron and Node.JS Developers.

Install

$ npm i windows-environment

Environment Variable Management operations offered by this library:

  1. Read all Environment Variables of the specified target (User and Machine).
  2. Read values of a specified Environment Variable.
  3. Create a new or append additional values to an already existing Environment Variable.
  4. Delete an Environment Variable by specifying its name.
  5. Delete a single value from an already existing Environment Variable.
  6. Finds broken paths in an Environment Variable (the path which no longer exits on your File System).
  7. Find duplicate values in an Environment Variable.
  8. Lastly optimize an Environment Variable's value which removes any redundancies (duplicate values) and broken paths.

Pros:

  1. Although the code is not written completely in Node.JS, still it guarantees speed and efficiency.
  2. Library size is only about 200 kb.

Cons:

  1. It requires .NET Framework 3.5 Client Profile as it is build upon C# Code.

Read all Environment Variables of the specified target (User and Machine):

// ES6 Destructuring Assignment
const { Env, Target, ExpandedForm } = require('windows-environment');

// Here the return value is an array of objects with name/value pairs.
let returnValues = Env.get({
    //specifies the target i.e. to search for environment variables inside user target
    target: Target.USER,
    //If this is set to no, all the values will be displayed as is.
    //If set to yes then, the values enclosed inside % symbol will take its long form
    //for e.g. %userprofile% will be C:\Users\UserName
    //for e.g. %systemroot% will be C:\Windows
    expandedForm: ExpandedForm.NO
});

//This looks pretty nice, give it a try.
console.table(returnValues);

// As the return value is an array(iterable object) it can be iterated using a for...of loop
for (let iter of returnValues)   {
    //iter is an object having name and value as keys
    console.log('Name : '+iter.name);
    console.log('Value : '+iter.value);
}

Read values of a specified Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, ExpandedForm } = require('windows-environment');

// Here the return value is a string cause the name field is specified.
let pathString = Env.get({
    //specifies the target i.e. to search for environment variables inside machine target
    target: Target.MACHINE,
    //If this is set to no, all the values will be displayed as is.
    //If set to yes then, the values enclosed inside % symbol will take its long form
    //for e.g. %userprofile% will be C:\Users\UserName
    //for e.g. %systemroot% will be C:\Windows
    expandedForm: ExpandedForm.YES,
    //This can be any name from that target like JDK, PATH, GH_TOKEN, TEMP, TMP, etc.
    name: 'PATH'
});

//Outputs the value of 'PATH' environment variable inside Machine target.
console.log(pathString);

Create a new Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, SetOperationType } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.set({
    //specifies the target, where to create the Environment Variable.
    target: Target.USER,
    //forcefully creates a new environment variable.
    //if an environment variable already exists with the same name
    //it simply overwrites it.
    setOperationType: SetOperationType.NEW,
    //the name of Environment Variable you want to create.
    name: 'PATH',
    //the value you want to give it.
    //Note: semicolons (;) are used for separating two paths if you have only single value then no need to use semicolons.
    value: 'F:\\Git\\PortableGit;C:\\Users\\Souleh\\AppData\\Roaming\\npm;C:\\Program Files (x86)\\Yarn\\bin'
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Append values to an already existing Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, SetOperationType } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.set({
    //specifies the target, where to create the Environment Variable.
    target: Target.MACHINE,
    //forcefully creates a new environment variable.
    //if an environment variable already exists with the same name
    //it simply overwrites it.
    setOperationType: SetOperationType.APPEND,
    //the name of Environment Variable you want to modify.
    name: 'PATH',
    //the value you want to give it.
    //Note: semicolons (;) are used for separating two paths if you have only single value then no need to use semicolons.
    value: 'C:\\Program Files\\Java\\jdk1.8.0_202\\bin'
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Delete an Environment Variable by its name:

// ES6 Destructuring Assignment
const { Env, Target } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.del({
    //specifies the target, form where to delete the Environment Variable.
    target: Target.MACHINE,
    //the name of Environment Variable you want to delete.
    name: 'JAVA_HOME',
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Delete a single value from an already existing Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target } = require('windows-environment');

// This method needs admin privileges for successful execution.
// Here the return value is the exit code either 0 for success or 1 for failure.
let exitCode = Env.del({
    //specifies the target, form where to delete the Environment Variable.
    target: Target.USER,
    //the name of Environment Variable's value you want to delete.
    name: 'PATH',
    //Note the value is case-sensitive,
    //if no matching value is found in the specified Environment Variable's value nothing will be deleted.
    value:'F:\\Git\\PortableGit'
});

//Use this line of code to know if operation was successful or not.
console.log((exitCode === 0) ? 'Operation was successful' : 'Operation failed!!!');

Finds broken paths in an Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, ExtOperationType } = require('windows-environment');

// Here the return value is an array of strings
let returnValues = Env.ext({
    //specifies the target i.e. to search for environment variables inside user target
    target: Target.USER,
    //finds all broken paths
    extOperationType: ExtOperationType.BROKEN,
    //name of the Environment Variable to find broken paths (the path which no longer exits on your File System).
    name: 'MAVEN_HOME'
});

// As the return value is an array(iterable object) it can be iterated using a for...of loop
for(let iter of returnValues)   {
    console.log(iter);
}

Find duplicate values in an Environment Variable:

// ES6 Destructuring Assignment
const { Env, Target, ExtOperationType } = require('windows-environment');

// Here the return value is an array of strings
let returnValues = Env.ext({
    //specifies the target i.e. to search for environment variables inside machine target
    target: Target.MACHINE,
    //finds all duplicates
    extOperationType: ExtOperationType.DUPLICATES,
    //name of the Environment Variable to finds duplicates values in.
    name: 'PATH'
});

// As the return value is an array(iterable object) it can be iterated using a for...of loop
for(let iter of returnValues)   {
    console.log(iter);
}

Optimize an Environment Variable's value:

// ES6 Destructuring Assignment
const { Env, Target, ExtOperationType } = require('windows-environment');

// Here the return value is a string
let optimizedPath = Env.ext({
    //specifies the target i.e. to search for environment variables inside user target
    target: Target.USER,
    //returns an optimized string removing any duplicates and broken paths
    extOperationType: ExtOperationType.OPTIMIZE,
    //name of the Environment Variable to optimize.
    name: 'PATH'
});

console.log(optimizedPath);