2

I have npm scripts in the package.json, I would like to provide custom parameter from the command line. I found many stackoverflow questions and answers but none of them works. I tried this solution:

  "scripts": {
    "foo": "echo $npm_config_foo && echo done"
  },

When I run the command:

$ npm run foo --foo=test

> echo $npm_config_foo && echo done

$npm_config_foo
done

As you can see, it does not echo test.

This is my configuration:

- OS: Windows
- Terminal: GitBash
- npm version: 7.14.0

I tried this, on Linux and it works. This is related to Windows.

2 Answers 2

5

The accepted answer is Great but does not work if the Repo is used by both Windows and Linux Users. If so than I recommend to use cross-var (https://www.npmjs.com/package/cross-var)

Just add it into the beginning of the script (after you added it to your dependencies)

"scripts": {
  "foo": "cross-var echo $npm_config_foo && echo done",
},
Sign up to request clarification or add additional context in comments.

Comments

3

The solution is to use % instead of $.

  "scripts": {
     "foo": "echo %npm_config_foo% && echo done",
  },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.