Use variables in your package.json

tykok

Tykok

Posted on May 11, 2022

Use variables in your package.json

Yes, you can use variable in your package.json file

So I'm gonna show you how you can use variables in this file.

What is the "package.json" ?

The package.json is like the official NodeJS website defines it to us :


A kind of a manifest for your project. It can do a lot of things, completely unrelated. It's a central repository of configuration for tools, for example. It's also where npm and yarn store the names and versions for all the installed packages.

For more precision in the package.json you can declare :

  1. The name of your application
  2. The version
  3. The Licence
  4. The description of your project
  5. The dependencies of your project (for production, and development)
  6. Script for your project (Run project, run tests, run Lint, run the build, ...)
  7. The engine of tools (Node, npm, ...)
  8. The author name of the project
  9. All contributors

And a lot of other things...

Why use variables into my package.json

Imagine, you define a command in your scripts section into the package. Jason of your own project to use many Bash files you created and has really useful for your project.

And all the Bash files are in the .bin repository, inside your project.

Your script section gonna be like that :



{
    ...
    "scripts": {
        "bash1": "bash .bin/yourFirstBash.sh",
        "bash2": "bash .bin/yourSecondBash.sh",
        "bash3": "bash .bin/yourThirdBash.sh",
    }
    ...
}


Enter fullscreen mode Exit fullscreen mode

All it's ok, but now if for any reasons you need to move this bin into another directory you need to change your package.json for each line.

And here you can use variables to change quickly the destination of your Bash script 😄.

How to use variables in your package.json?

To use variable, you need to declare a section named config (or something else, but not a name was already taken by the package.json). And in this section, you can declare ALL YOUR VARIABLES :



{
    ...
    "config": {
        "path": ".bin",
        "entrypoint": "server.js",
        "testFolder": "src/test",
    }
    ...
}


Enter fullscreen mode Exit fullscreen mode

And to use a variable, you just need to write $npm_package_ + config (name of the section) + _path (name of the variable).

Here is an example with the previous section of scripts:



{
    ...
    "scripts": {
        "bash1": "bash $npm_package_config_path/yourFirstBash.sh",
        "bash2": "bash $npm_package_config_path/yourSecondBash.sh",
        "bash3": "bash $npm_package_config_path/yourThirdBash.sh",
    }
    ...
}


Enter fullscreen mode Exit fullscreen mode

And you what do you think about using variables into your package.json ?

Main source : https://brianchildress.co/variables-in-package-json/

💖 💪 🙅 🚩
tykok
Tykok

Posted on May 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related