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

win-node-env

v0.6.1

Published

Set NODE_ENV variable before a command on Windows

Downloads

14,089

Readme

win-node-env

Run npm scripts on Windows that set (common) environment variables.

Note: Works only in cmd.exe, not in PowerShell. See #6

Problem

If you're on Windows, you've probably encountered an error like:

'NODE_ENV' is not recognized as an internal or external command, operable program or batch file.

which comes from an npm script in your project set up like this:

"scripts": {
  "build": "NODE_ENV=production babel src --out-dir dist"
}

Setting NODE_ENV=production before command babel doesn't work on Windows.

You might use cross-env but that involves changing your npm scripts and getting Mac/*nix users onboard.

Solution

win-node-env creates executables like NODE_ENV.cmd that sets the NODE_ENV environment variable and runs the rest of the command.

Install

You may install it globally:

npm install -g win-node-env

Or you may include it in your project's or your library's optional dependencies:

npm install --save-optional win-node-env

It won't install on any other OS than Windows.

Usage

Just install it and run your npm script commands, it should automatically make them work.

NODE_ENV=production cmd /c echo %NODE_ENV%

should output:

production

Apart from NODE_ENV there's a few more commonly used env vars:

  • DEBUG
  • ENV
  • PORT
  • NODE_OPTIONS

You can also use multiple env vars, as long as the first one is one of the above

NODE_ENV=production MY_VAR=something cmd /c echo %MY_VAR%

Bonus

It now also supports ; character!

ENV=1 command; command

Although any &&, ||, and & might break it.

ENV=1 command && command ; command
^^^^^^^^^^^^^    ^^^^^^^^^^^^^^^^^
processed by     not processed by
win-node-env     win-node-env

Tip: to add even more custom variables

If you'd like to add even more custom variable(s) (that you can specify as first) you can do so like this.

Suppose you want to add MY_VAR, place a file named MY_VAR.cmd where it can be accessed by your command prompt. (when you enter a command in your command prompt, say MY_VAR, it looks for a file with the name MY_VAR.cmd in a list of pre-defined paths. This list of pre-defined paths resides in the environment variable PATH. You can edit it to include the path containing your MY_VAR.cmd file)

Make sure this module is installed globally.

Then simply put the following code in this file:

  • MY_VAR.cmd

    @ECHO OFF
    SET NODE_PATH=%APPDATA%\npm\node_modules
    node -e "require('win-node-env')('%~n0')" X %*
    • NODE_PATH tells require where to look for.

    • %APPDATA%\npm\node_modules is generally where your globally installed modules live

    • %~n0 expands to the current file's name (without extension), i.e. 'MY_VAR'

    • X is a dummy argument that's just needed for some reason

    • %* expands all the arguments passed to the batch file, and passes them on to this module

You can use the same contents of this file for any other variable names as well, i.e. just copy this file and change the filename.