Unable to retrieve command line arguments while running the npm script

As a newcomer to electron, I am currently working on using it to package a react-based app. In an attempt to run electron with a script entry in my package.json file:

"electron-dev": "concurrently \"cross-env BROWSER=none npm start\" \"wait-on http://localhost:3000 && electron . $npm_config_input \"",

This command should execute electron.js (which is specified as the "main" earlier in the package.json), but the challenge arises when trying to pass a command line argument. Although some references suggest that $npm_config_input can be used to pass arguments like this:

% npm run electron-dev --input=file.tif

I have noticed that $npm_config_input remains literal and does not expand for me. Consequently, electron.js receives the exact string $npm_config_input which points to confusion on why it fails to work.

An alternative solution could be:

% npm run electron-dev -- --input=file.tif

However, I am uncertain how to link the input argument to the second concurrently started command. It would be ideal if there was a way to reference something like $1 or $npm_config_input within its definition. Does anyone know of a workaround for this issue?

I am utilizing Windows 10 with git bash for this setup. Most other functionalities seem to be working fine. The environment has nodejs version 12.16.2 installed. Thank you in advance!

Answer №1

Great job, you're so close! Just make sure to utilize process.argv.

As an example, create a file called cmd.js with the following content:

console.log(process.argv.slice(2));

Next, set up a script in your package.json by adding this snippet:

"scripts": {
  "foo": "node cmd.js"
}

Give it a try now...

$ npm run foo -- arg1 arg2

> <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5939a9ab5c4dbc5dbc5">[email protected]</a> foo /tmp/foo
> node cmd.js "arg1" "arg2"

[ 'arg1', 'arg2' ]

Answer №2

After some investigation, I stumbled upon a solution. While working with electron.js, I decided to check process.argv to see if it held any valuable information. It turned out that process.env.npm_config_input contains the value file.tif when I executed this command:

% npm run electron-dev --input=file.tif

This method seems to be effective for me. It's still puzzling why the other methods I researched and attempted were not successful.

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

Develop a JavaScript library for use in the npm ecosystem

I have developed a minimalist JavaScript library that includes common functions: !!window.JsUtils || (window.JsUtils = {}); JsUtils = (function () { "use strict"; return { randomHex: function (len) { var maxlen = 8; ...

The challenge of managing timeouts in Node.js HTTP requests

When using the npm request module to call an HTTP API, I am encountering a strange issue. It is working for one URL but not for another on the same domain. Every time I attempt to access the second URL, I receive a timeout error. Strangely, the same URL ...

Error message appears: "Unable to access 'upgrade' property of undefined" upon launching Vue application

Everything was running smoothly with my project for a few months. I could easily execute npm run serve without any issues, even when switching networks. But now, no matter what I do, I can't seem to get the server to start again. The error message I&a ...

I'm having trouble launching the Vue UI after successfully installing the Vue CLI - any ideas why?

After installing the following versions: node 8.11.2 npm 6.3.0 I proceeded to install the Vue CLI (@vue/[email protected]): npm i -g @vue/cli However, upon running the command below: vue ui An error message was displayed as follows: Error: Cann ...

Securing Artifacts in GitHub Actions: Best Practices for Keeping Your Data

I have a unique project in mind for a single-page application hosted on GitHub. The application will interact with a REST API, and I am using Vite as the build tool. One challenge I'm facing is that all environment variables are included in the output ...

How can I determine the version of an NPM package if I can't display it using the nodemon --version command?

I attempted to execute some code in my hyper-terminal by deleting nodemon and then reinstalling it. However, despite these efforts, I am still unable to retrieve the version of nodemon. The error message I received reads as follows: "C:\Users&bso ...

Creating a release of an Angular 12 library using Ivy and sharing it on npm

Recently, I had the task of updating a library to angular 12. After successfully compiling it with Ivy full compilation mode, I realized that it cannot be published on npm in this state. Following suggestions from various sources, I tried setting "enableI ...

Error encountered while trying to install npm for a web project

While attempting to install npm modules, I am encountering the following issue: PS C:\Users\maxzag\Desktop\svoi> npm install npm ERR! Cannot read property '0' of undefined npm ERR! A complete log of this run can be found ...

Jest tests failing due to broken linked library

We developed a React library for use in another application. However, when attempting to link the library using npm link ../myLibrary, Jest tests fail with an error message: TypeError: Cannot read property 'webpackChunk_my_library' of undefined ...

Unable to launch electron using "npm start" while enabling X11 forwarding

I am attempting to launch an electron app over ssh with X11 forwarding on a headless machine. Specifically, I am using the electron-quick-start repository. The app works fine when I run it by executing electron . in the cloned folder. However, when I try t ...

Node.js application encounters a challenge with Swagger UI requirement not being met

My Node.js application utilizes Electron v15.3.1 and includes a Swagger UI for OAS file viewing, specifically version 4.6.1. Due to a security vulnerability in the Swagger UI, I need to upgrade it. Initially, I attempted to resolve the issue by running np ...

Guidelines for creating a binary release of Node.js with native modules

Currently, I am in the midst of exploring the world of Node.js projects, delving into different bundlers and various other components. One interesting concept that came to mind is the idea of bundling Node.js into a single binary for Linux, macOS, or Windo ...

Optimizing SCSS Styles for WordPress Websites

Currently, I am in the process of setting up a configuration in Visual Studio Code for a project to adhere to WordPress standards when formatting SCSS code. Despite my efforts so far, I have not encountered any errors and the code is not being formatted up ...

Gzip is generating inaccurate compression ratios

Having an issue with script to build and gzip. vite build && gzip -9 -r dist && gzip -l -r dist The output from the last command is displayed here gzip -l -r dist compressed uncompressed ratio uncompressed_name ...

Are the dependencies of a Node module not updating properly after being updated or installed?

I recently integrated react-highcharts into my application. After using npm install react-highcharts, the installation was successful, but with a warning message: found 1 high severity vulnerability, run `npm audit fix` to fix them, or `npm audit` for deta ...

Encountering errors while attempting to set up a Vuetify project using npm installation

These were the steps I followed during the installation of a Vuetify project: -npm install -g vue-cli -vue init vuetifyjs/webpack my-project One of the errors that stood out among all others was: -npm ERR! Unexpected end of JSON ...

I used npm to install AngularJS and then included AngularJS in my application

My goal is to set up AngularJS v1.5.x using npm and integrate it into my application for seamless utilization. Most tutorials opt for downloading the Angular Version from angularjs.org and manually adding it to the index.html within a <script></sc ...

Running npm install within the user's data directory

My goal is to set up a launch template in AWS with specific user data included: #!/bin/bash home=/home/ec2-user nodev='8.11.2' nvmv='0.33.11' #install node su - ec2-user -c "curl https://raw.githubusercontent.com/creationix/nvm/v ...

Encountering a "Type expected" error in NPM when compiling a React/Next.js project following the integration of use-places

While building my React/Next.js project, I encountered an error. Even after attempting to open VS Code as an administrator, the issue persisted. ./node_modules/use-places-autocomplete/dist/index.d.ts:21:24 Type error: Type expected. 19 | export type S ...

Are you struggling with perplexing TypeScript error messages caused by a hyphen in the package name?

After creating a JavaScript/TypeScript library, my goal is for it to function as: A global variable when called from either JavaScript or TypeScript Accessible via RequireJS when called from either JavaScript or TypeScript Complete unit test coverage Th ...