Access variables in npm scripts

Increase the flexibility of your npm scripts

Kevin Kreuzer
5 min readMay 22, 2019

A while ago, almost all build tasks were managed by tools like grunt or gulp.

Depending on the project, people needed to understand the configurations and structures of the build tool which introduced some unnecessary complexity.

Since almost everybody is using npm as their package manager, it makes sense to profit from its ability to run scripts. npm scripts are managed inside our package.json and can be executed with the npm run command.

Compared to grunt or gulp files, they are easier to understand and maintain.

Flexible npm scripts

What do we mean when we talk about flexible npm scripts? We talk about scripts that can access variables and pass them to another script. Let’s have a look at the following graphic, which illustrates this concept further.

We have an environment with a variable. In this example, a variable called A which has the value 5. We want to pass in this variable to our npm script, access it, and pass it to the third-party library which we call in our script.

--

--

Kevin Kreuzer

Passionate freelance frontend engineer. ❤️ Always eager to learn, share and expand knowledge.