Skip to content
Learn with RV – Tech Blog
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

  • Home
  • Who am I?
  • 1-on-1 Mentoring
  • 18 Testimonials
  • YouTube
  • LinkedIn
  • Contact
Learn with RV – Tech Blog

#testautomation #qa #programming #linux #devops

cross-env-featured
June 10, 2025June 10, 2025

Using cross-env in Your Node.js Projects: A Practical Guide

When developing cross-platform Node.js applications/testing frameworks, environment variables can sometimes cause frustrating issues—especially when switching between Windows, macOS, and Linux. That’s where cross-env comes to the rescue.

In this tutorial, we’ll explore what cross-env is, why you need it, and how to integrate it into your project.

What is cross-env?

cross-env is a simple npm package that allows you to set environment variables across different platforms in a consistent way.

Why is it needed?

Different operating systems handle environment variables differently:

  • Unix-based systems (Linux/macOS): You can set variables like NODE_ENV=production.

  • Windows: That same syntax doesn’t work. Windows uses a different method (set NODE_ENV=production).

 

So if you have a script like this in your package.json:

“scripts”: {
  “start”: “NODE_ENV=production node server.js”
}

It will break on Windows. Enter cross-env.

Installing cross-env

To install it, use either npm or Yarn:

npm install --save-dev cross-env
# or
yarn add --dev cross-env

This adds it as a development dependency, since it’s only needed during script execution.

How to use it

Simply prefix your command with cross-env, and it will take care of setting the environment variable appropriately for any OS.

Without cross-env (UNIX-only)

"scripts": {
"start": "NODE_ENV=production node server.js"
}

With cross-env (cross-platform)

"scripts": {
"start": "npx cross-env NODE_ENV=production node server.js"
}

That’s it. Now it works the same on Windows, macOS, and Linux.

Enjoyed this article? Make sure to subscribe to my YouTube Channel for more Test Automation tutorials, and follow me on LinkedIn and Twitter for regular insights.
Looking to improve your test automation skills?
I’ve created a personalized 1-on-1 Mentoring program refined to boost YOUR CURRENT skills. Reach out at iamrv@razvanvancea.ro for more details and together will create a learning path adapted to your skills and goals that you are aiming for, in a timely-efficient manner🚀

Post Views: 76

Related

Share this article:
Programming

Post navigation

Previous post

Recent Posts

  • Using cross-env in Your Node.js Projects: A Practical Guide
  • How to copy-paste text from clipboard using TestCafe
  • How to copy-paste text from clipboard using Cypress
  • How to repeat any CLI Command for X times using NPM repeat-cli-command
  • k9s: A Guide to Master Kubernetes Clusters from your Terminal

Recent Comments

  1. Paul on Web Accessibility: A step-by-step guide to Testing with pa11y
  2. Automated Tests for website Accessibility with Axe and TestCafe - Learn with RV - Tech Blog on How to generate E2E TestCafe Framework in seconds
  3. RV on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  4. Adrian Maciuc on Exploring Faker.js: A Powerful Tool for Generating Realistic Random Test Data
  5. Nick on Cypress vs Playwright vs Testcafe – which framework is faster?

Archives

  • June 2025
  • March 2025
  • February 2025
  • January 2025
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023

Categories

  • Linux
  • Programming
  • QA
  • Tools
  • Uncategorized
©2025 Learn with RV – Tech Blog | WordPress Theme by SuperbThemes