Pass multiple variables to a package.json script is possible by utilizing the npm run command and

Within my package.json file, I have the following section (NOT BASH, NOT SH, NOT ZSHELL, NOT FISH).

Now that we've established this is indeed my package.json file, let me showcase its contents:

package.json

"scripts": {
"dev": "NODE_ENV=myValue myProgram"
}

I am looking to add additional variables (e.g. MYVAR=myOtherValue) to the above file, which, as mentioned before, is my package.json file. How can I achieve this (adding more variables to my package.json file)?

To clarify, I am not inclined to refer to the manpage of bash, zshell, fish, or sh. This is precisely why I am posing the question here instead of consulting the manpage - if that were the case, I wouldn't be seeking answers here and would resort to reading the manual. Your understanding is greatly appreciated.

Answer №1

Ensure your script includes the following:

"dev": "NODE_ENV=myValue MYVAR=myOtherValue myProgram"

You have the ability to include multiple environment variables separated by spaces.

This concept is derived from the typical behavior in command line terminals such as bash, where you can define and access various environmental settings on the spot:

FOO1=baz FOO2=fnord FOO3=baz env | grep FOO
FOO1=baz
FOO2=fnord
FOO3=baz

Answer №2

I agree with Stevek on this topic. I found the answer provided frustrating due to its references to bash, considering that I am working on a Windows system using Powershell. My goal is to pass multiple environmental variables and ensure compatibility with Windows systems utilizing Powershell. Any mention of *nix commands only serves to confuse the issue unless it pertains to enabling cross-system functionality.

As this answer remains incomplete, I will share what I have discovered:

This resource explains how to pass a single variable: How to set environment variables from within package.json

 "start": "set NODE_ENV=YOURENV&&node start.js"

The question remains: how can multiple variables be passed? The search continues.

It has been suggested that using 'cross-env' is the preferred method: cross-env NODE_ENV=production my-command

Further details are available in this post: https://github.com/kentcdodds/cross-env/issues/15

"scripts": {
  "debug": "cross-env NODE_PATH=. DEBUG=app:* nodemon bootload.js"
}

However, it should be noted that 'cross-env' does not utilize PowerShell but instead leverages the deprecated cmd shell in Windows. This brings us back to the primary concern [unless modifications are made as outlined here: https://github.com/kentcdodds/cross-env/issues/192#issuecomment-513341729]

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Learn how to install an NPM package as if it were any other

Looking to install the package com.cordova.plugin.cache along with its content, also requiring it to be accessible as cordova-plugin-cache. An example of how the package.json should be structured: "dependencies": { // Both entries are essentially the ...

Encountered an issue loading next.config.js during the build process with GitHub actions

Currently, I am utilizing a straightforward ssh deploy action along with a bash script designed to both build and restart the pm2 frontend process. Everything seems to be running smoothly when executing the script directly on the ec2 instance. However, is ...

Encountering an uncaught SyntaxError due to an unexpected token < while working with AngularJS2 on a local

I am currently facing an issue while trying to run my angularjs2 project locally using nodejs. I can successfully run it using npm start but encountering errors when attempting to use node app.js. The error message that pops up states: "systemjs.config.js ...

npm is currently unable to fetch packages from the registry at registry.npmjs.org

I am encountering the following error message when trying: npm search rollup npm ERR! pna.nextTick is not a function npm ERR! code ECONNRESET npm ERR! network tunneling socket could not be established, statusCode=503 npm ERR! network This is a problem re ...

Pre-request Middleware Executed in ExpressJS prior to any requests

Currently, I am working on developing a NodeJS Api and encountering some difficulties with middlewares. User accessibility to routes is based on their roles, which can be either Admin or SuperAdmin. The structure of my permission middleware is as follows: ...

Troubleshoot: Problem with installing Angular-CLI

When attempting to install "angular-cli" by running the command "npm install -g angular-cli@latest" in my Administrator command prompt, I encountered the following error: C:\Windows\system32>npm install -g angular-cli@latest npm WARN addRemo ...

passport not initializing session with requests

I am currently utilizing passportJS for managing login and persistent sessions on the server backend. While everything functions properly when sending requests from the server frontend (a webpage that I didn't create and lack sufficient knowledge to ...

The requested resource does not contain the 'Access-Control-Allow-Origin' header. It has already been included in the headers

Despite adding the 'Access-Control-Allow-Origin': '*' header to my Axios request, I am still encountering the same error. When trying to access 'http://backendUrlEndPoint' from origin 'https://frontendURL', the COR ...

Choose SQL query for grouping by nested JSON properties

My select query is providing data in the following format: [{ id: 'CB2FD8B7-7E6D-4BF3-8E73-70D41FFBE456', products: '[{"product_id":"22061DA1-5D64-475A-B36A-140ECFE8D6B7"}]', falconpay_api_response: null, dhl_updated_by: null } ...

What could be causing the Type Error in jsdom?

When attempting to parse a remote site using jsdom, I encountered an error when trying to use it with node-webkit.js: I received the following error: "Uncaught TypeError: A 'super' constructor call may only appear as the first statement of a f ...

Learn how to securely transmit data using basic authentication in Node.js with the node-fetch module

Having trouble with this code. I am facing an issue where a "post" request with basic authentication returns a 200 status, but no response data is received. Surprisingly, when the same code is executed without auth credentials, it works perfectly fine. l ...

Solving the error message "window is not defined" in Nextjs

Hey, I'm attempting to create a component similar to [this tutorial][1] in my NextJS app but I'm running into an error ReferenceError: window is not defined //Navbar.js import styles from "../styles/Navbar.module.css"; export default fu ...

Using both withNextIntl and withPlaiceholder simultaneously in a NextJS project causes compatibility issues

I recently upgraded to NextJS 14 and encountered an issue when deploying my project on Vercel. The next.config.mjs file in which I wrapped my nextConfig in two plugins seemed to prevent the build from completing successfully. As a workaround, I decided t ...

Guide to declaring transitive package dependency when upgrading a package

During the process of updating packages with the ncu command in my Node.js project that utilizes npm, is there a way to specify upgrading all packages except for React 17 and any packages that rely on React 17? Essentially, I am looking to exclude a partic ...

What is the process for displaying node_modules directories in a json/javascript document?

Exploring ways to showcase the dependencies within my d3.js tree, I am curious if it's possible to dynamically list the dependencies' names in a JSON file or directly within the javascript file. I'm puzzled by how JavaScript can access folde ...

I'm having some trouble with my middleware test in Jest - what could be going wrong?

Below is the middleware function that needs testing: export default function validateReqBodyMiddleware(req: Request, res: Response, next: NextFunction) { const { name, email }: RequestBody = req.body; let errors: iError[] = []; if (!validator.isEmai ...

Using Sequelize for a many-to-many relationship with additional columns

Following extensive research, I was unable to find any relevant information pertaining to my issue. The scenario involves an M:M relationship that is already operational with Sequelize (SQLite): return User.find({ where: { _id: userId } }).then(user => ...

"Accessing your account only requires a simple two-click login process

Why do I need to click the log in button twice for data validation on my forum website? While designing a forum website, I noticed that users have to click the log-in button twice before the system validates and processes the input data. Below is the code ...

Error in binding the port during Heroku deployment with webpack

I am facing an issue while deploying my package.json with all the dependencies and a start script using webpack on Heroku. The error message I get is R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch. "scripts": { "star ...

A guide to sharing session variables with express views

Struggling to access session variables in EJS views and encountering various challenges. To locally access req.session, I've implemented middleware as outlined in this guide on accessing Express.js req or session from Jade template. var express = re ...