question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing arguments within a script

See original GitHub issue

Any idea how I’d replace all instances of breaknotes in this script with a argument?

I’m very close to writing a build script to write build scripts for each of my components. (not a good idea)

`scripts`:{
    "update:breaknotes": "cd ./generators/ && node component.js breaknotes && terminal-notifier -message 'breaknotes updated'",
}

sadly npm config set component "breaknotes doesn’t update the local package config even without the --global flag. $npm_package_config_component only pulls in the local

  "config": {
    "component": "breaknotes"
  },

Is there a way to write to the local config with a command because then we could write

npm config set component "breaknotes" && npm run update:component

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
keithamuscommented, Dec 10, 2014

As I mentioned in the article you can override config vars using command line switches. So let’s say you had a package json something like:

"name": "myproject",
"config": {
    "component": "breaknotes"
},
"scripts" :{
    "update": "cd ./generators/ && node component.js $npm_package_config_component && terminal-notifier -message '$npm_package_config_component updated'",
}

You could run npm run update --myproject:component=foo and it would override the $npm_package_config_component var to be foo.

The reason your permanent npm config set command isn’t working, is because it needs the project namespace (it needs to be prefixed with the project name, plus a colon). So given my above example, I’d run the command npm config set myproject:component "foo" to set it permanently in my local ~/.npmrc

1reaction
keithamuscommented, Apr 27, 2015

Ah, unfortunately variables won’t work in Windows. You can use npm config still, but you have to use it inside of a Node.js script, so this will work:

{
  "name": "example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "config":{
    "var": "hello world",
    "VAR": "HELLO WORLD"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "example": "node -p 'process.env.npm_package_config_var'",
    "caps": "node -p 'process.env.npm_package_config_VAR'"
  },
  "author": "",
  "license": "ISC"
}

Alternatively if you’re ONLY using windows for this project, you can use the Windows variable syntax of %VAR%. I haven’t tested this but it should work:

{
  "name": "example",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "config":{
    "var": "hello world",
    "VAR": "HELLO WORLD"
  },
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "example": "echo %npm_package_config_var%",
    "caps": "echo %npm_package_config_VAR%",
    "arg": "node -e 'console.log(process.argv)' %npm_package_config_var% %npm_package_config_VAR%"
  },
  "author": "",
  "license": "ISC"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass and use arguments in shell script - Educative.io
Inside the script, we can use the $ symbol followed by the integer to access the arguments passed. For example, $1 , $2...
Read more >
How can I pass a command line argument into a shell script?
You can access passed arguments with $n where n is the argument number - 1, ...
Read more >
How to Use Command Line Arguments in a Bash Script
Arguments passed to a script are processed in the same order in which they're sent. The indexing of the arguments starts at one,...
Read more >
Passing Arguments to a Script - Micro Focus
In the Arguments field in the Run Test Case dialog box. · In Silk Central. Select the Properties tab of a Silk Test...
Read more >
How to Pass Arguments to a Python Script from the Command ...
In Python, arguments are passed to a script from the command line using the sys package. The argv member of sys ( sys.argv...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found