Nodachisoft Nodachisoft logo, Katana Sword Icon
  
@Amaji✎ Update December-16-2021

How to pass arguments to npm run script

How to pass arguments to npm run script

Abstract

when you want to variables to run script with npm, you can pass arguments from npm run command.

This article describe how to pass to npm run.

code

In package.json file, you can add sample npm-script like this:

 
package.json
  "scripts": {
    "hello": "node ./batch/helloexample.js"
  }

and In project, write the code at /project_dir/batch/helloexample.js.

sample code is here.

 
helloexample.js
console.log(process.argv);

now you can run this hello npm-run-script in your windows command prompt or linux, MacOS shell.

run
> npm run hello argument1 argument2

> project_name@version hello
> node ./batch/hello.js

[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\02_data\\01_dev\\03_nodachisoftweb\\batch\\hello.js'
  'argument1',
  'argument2'
]

You can see that the arguments you wrote when you ran the npm run command are passed to process.argv.

Passing hyphenated arguments

For example, suppose you want to pass a string like "--option:fix" as an argument to npm run. In this case, you need to add "--" like "npm run -- --option:fix" to pass it successfully.

Passing a spaced argument

By enclosing a string containing whitespace in double quotation marks ("), you can use the You can pass a string with spaces as a single argument.

run
> npm run hello "arg ument !!"

> project_name@version hello
> node ./batch/hello.js

[
  'C:\\Program Files\\nodejs\\node.exe',
  'C:\\02_data\\01_dev\\03_nodachisoftweb\\batch\\hello.js'
  'arg ument !!',
]

That all, have fun life with npm-run-script!!

Websites and references that were very helpful.

Page History

date modification
none
 
 
Message sent

Thank you for your message.

Something error has occured!

Sorry. The Error has occurred.We apologize for the inconvenience.Please try again in a few minutes or contact us via DM below.

Twitter:@NodachiSoft_eng
Name:
 
Replay To:
 
Message:
 
Back
Check the content!

Send the following information to us. If you are happy with your submission, please click "Send". If you want to modify it, please click "Back".

Name:
 
Reply To:
 
Message:
 
Enter a confirmation key to make sure that you are not operating from a Robot.
Confirmation Key is 95
Back
 / 
Go to Confirmation
Entry fields
Go to Confirmation

There are 5 articles that may be relevant!

How to copy directory recursively in javascript on nodejs.

How to copy directory recursively in javascript on nodejs.

#Javascript#Nodejs✎ 2021-12-16
Describes How to copy directory recursively with javascript on nodejs , with sample code that can be copied and pasted.
Table Of Contents
How to pass arguments to npm run script
How to pass arguments to npm run script
Abstract
Abstract
code
code
Passing hyphenated arguments
Passing hyphenated arguments
Passing a spaced argument
Passing a spaced argument
Websites and references that were very helpful.
Websites and references that were very helpful.
Page History
Page History
Nodachisoft © 2021