Introduction
When developing applications with Node.js, setting environment variables is crucial for configuring how your application behaves. For instance, setting NODE_ENV=production
can optimize your application’s performance by enabling production-specific settings such as minification or disabling verbose logging.
This tutorial will guide you through various methods to set the NODE_ENV
variable on Windows systems. We’ll explore command line techniques, using a helpful npm package for cross-platform compatibility, and configuring scripts in your package.json
.
Setting Environment Variables via Command Line
Using PowerShell
Windows has evolved from traditional CMD shells to more advanced environments like PowerShell. To set an environment variable using PowerShell:
- Open PowerShell.
- Execute the command:
$env:NODE_ENV="production"
This command sets the NODE_ENV
variable for your current session. Remember, this is a temporary setting and will reset when you close the PowerShell window.
Using Command Prompt (CMD)
If you prefer or need to use CMD:
- Open the Command Prompt.
- Set the environment variable with:
set NODE_ENV=production
This command also applies only to your current session in the Command Prompt.
Persisting Environment Variables
To make these variables persist beyond a single session, you can adjust them globally:
- Open the System Properties dialog by typing "environment" into the Start menu search box and selecting “Edit the system environment variables.”
- Click on "Environment Variables."
- Under “System variables” or “User variables,” click "New" to add
NODE_ENV
with its value asproduction
.
This method will ensure that NODE_ENV
is set for all future command prompt sessions.
Using Cross-Platform Solutions
For a seamless experience across different operating systems, you can use the npm package cross-env
. This tool provides an easy way to manage environment variables without worrying about platform-specific commands.
Installation and Usage
-
Install
cross-env
globally or as a dev dependency:npm install --save-dev cross-env
-
Use it in your npm scripts within
package.json
:"scripts": { "start": "cross-env NODE_ENV=production node index.js" }
-
Run your application with:
npm start
This approach ensures compatibility across Windows and Unix-like systems.
Combining Commands in Scripts
If you prefer to set environment variables directly when running a command, use the &&
operator in CMD:
set NODE_ENV=production && node index.js
Ensure there are no spaces around &&
for it to work correctly.
Alternatively, configure your package.json
with a script specifically for Windows:
"scripts": {
"start_windows": "set NODE_ENV=production&&node index.js"
}
Run this command via npm:
npm run start_windows
Using NPM’s Built-in Environment Script
NPM provides a built-in way to manage environment variables using env
scripts. This method is efficient and platform-independent.
-
Define an npm script in your
package.json
:"scripts": { "start": "node index.js", "start_with_env": "npm run env NODE_ENV=production node index.js" }
-
Execute it with:
npm run start_with_env
This command uses NPM’s env
to pass environment variables directly to your script.
Conclusion
Setting the NODE_ENV
variable is a common requirement when deploying Node.js applications, especially for distinguishing between development and production environments. This tutorial covered various methods to achieve this on Windows, from using native commands in PowerShell or CMD to leveraging npm packages like cross-env
. Choose the method that best fits your workflow and project requirements.