Command line interface to send parameter value to npm script

I am working on a typescript project and I have an npm script defined in my package.json file as follows:

"test:add": "cross-env some-static-var=value npm run some-command"

Now, I need to pass a variable value when I run this script from the command line interface (CLI). I just want to be able to pass a value, even if no specific variable name is provided in the CLI. Essentially, I would like to run the script like this:

npm run test:add --additionalValue

I am struggling to figure out how to pass and access this value from the CLI. I have tried various methods such as checking process.env, process.argv, and process.arg0 but none of them seem to contain the value "additionalValue." Can someone please help me with this issue?

Answer №1

It appears you are on the right track, here is a basic example of how to implement that function.

The package.json file contains this script:

"scripts": {
    "helloWorld": "node hello-world.js"
  },

The contents of hello-world.js would be as follows:

const arg = process.env.npm_config_hello || "";

console.log(arg);

To execute the command using npm, use the following syntax:

npm run helloWorld --hello="Hello World!"

This will result in the output being displayed as Hello World!

For a demonstration, you can view this sandbox: https://codesandbox.io/p/devbox/cg2sg8

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

Utilize CircleCI for seamless automated publishing of packages to NPM

Is it possible to configure automated deployments for a public NPM package using CircleCI? I am looking to have CircleCI automatically publish the package to NPM whenever there is a change in the version number specified in the package.json. Can this be ...

Differences Between Angular 2 Reactive Forms and Template Forms

We are embarking on a new Angular 2 project and are deliberating on whether to opt for Reactive Forms or Template Forms. If you want to learn more, you can refer to this article: https://angular.io/guide/reactive-forms From what I understand, the main adv ...

Which TypeScript data type is most appropriate for referencing the match object within my props?

When working with React containers/components, how can I properly reference the 'match' component from React Router DOM? interface Props { match: any // <= What data type should I use in place of 'any'? } export class ProductCont ...

Converting a JSON array stored in a local file to a TypeScript array within an Angular 5 project

I'm currently working on developing a web app using Angular 5. My JSON file has the following structure: [ { "id": 0, "title": "Some title" }, { "id": 1, "title": "Some title" }, ... ] The JSON file is store ...

Beginning the React Script

Every time I try to run the npm start script, I encounter an error. Despite deleting the node_modules folder and package-lock.json file, then reinstalling them, the error persists ...

Warning: Unsupported engine for version 10.0.0 of [email protected] - required engine is {"node":">=10.0.0"}

{ "name": "express", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test speci ...

Add a class to a button in an Angular btn-group if a specific string is found within an

I am currently working on a project where I have multiple buttons that need to toggle an active class when selected in order to change their color. Below is a snippet of what I have: In the array called 'selected', I have: this.selected = [&ap ...

The Freemode feature in SwiperJS is not functioning properly when used with React TypeScript

Having a slight issue with SwiperJS. Using version 10.1.0 and the following code : import { Swiper, SwiperSlide } from "swiper/react"; import "swiper/css"; export default function Discover() { return ( <> ...

Error encountered while trying to execute Node.js project - npm error code EBADPLATFORM

Just dipping my toes into nodeJS, so bear with me on this question. Recently set up nodejs, node-gyp, npm, git, Python and MS Visual Studio C++ Express. Running "npm install" gives me the following results: npm WARN package.json No license field. > ...

Angular routing is showing an undefined ID from the snapshot

When a user attempts to update a student, I pass in the student ID. The update successfully redirects to the updateStudent page and displays the student ID in the browser link. However, within my app component, it shows as undefined. Student View componen ...

Retrieve the service variable in the routing file

How do I access the service variable in my routing file? I created a UserService with a variable named user and I need to use that variable in my routing file. Here is the approach I tried, but it didn't work: In the routing file, I attempted: cons ...

Loop through a collection of elements of a certain kind and selectively transfer only certain items to a different collection of a different kind

When working with typescript, I am faced with the challenge of dealing with two arrays: interface IFirst{ name: string; age: number } interface ISecond { nickName: string; lastName: string; } myFirstArray: IFirst[]; mySecondArray: ISe ...

What is the best way to implement switchMap when dealing with a login form submission?

Is there a better way to prevent multiple submissions of a login form using the switchMap operator? I've attempted to utilize subjects without success. Below is my current code. import { Subject } from 'rxjs'; import { Component, Output } ...

Adding a line break ( ) in a paragraph within a TypeScript file and then transferring it to HTML does not seem to be functioning properly

Angular Website Component: HTML file <content-section [text]="data"></content-section> TypeScript file data = `Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's stand ...

Is it recommended for the main, module, and browser properties in package.json to reference the minified version or the source

When developing a JavaScript library, it is important to consider the structure in which it will be published. This typically includes various bundles such as CJS, ESM, and UMD, each with their own source, minified, and map files. my-package\ dist& ...

What steps do I need to take to implement Dispatch in a React Native project?

Context App.tsx import React, { createContext, useContext, useReducer, useEffect, ReactNode, Dispatch, } from "react"; import AsyncStorage from "@react-native-async-storage/async-storage"; import { StateType, ActionType } f ...

Is there a way to adjust the dimensions of my Angular npm slider?

This snippet shows the Angular code I came across this npm slider on npm.js Struggling to adjust the margin of the images as they are sticking, also unsure about modifying the size and other elements using https://www.npmjs.com/package/ng-image-slider. A ...

Struggling to comprehend the node.js walker concept

I am struggling to grasp the concept of how the signature/header of the node.js walker functions. I understand that a walker can go through a directory and apply filters, but I'm unsure about how the signature of the .on function works. For example: ...

"Can someone point me to the location of $(System.DefaultWorkingDirectory) within an Azure DevOps

I am having trouble displaying junit.xml test results within an Azure DevOps pipeline as the task cannot locate it. Can someone advise on the path starting from $(System.DefaultWorkingDirectory) to locate the junit.xml file generated after executing an n ...

Converting an array of object values to an Interface type in Typescript

In my JSON document, I have an array named dealers that consists of various dealer objects like the examples below: "dealers" : [ { "name" : "BMW Dealer", "country" : "Belgium", "code" : "123" }, { "name" : ...