Use variables in your package.json
Tags: javascript, programming, tutorial, node
Published: 2022-05-11 17:36:14

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

<div align="center">
<img src="https://media4.giphy.com/media/3o7527pa7qs9kCG78A/giphy.gif?cid=ecf05e47flesuf0ff2zn2pdq2sodkx2agghc3h0r1sbp1l4m&rid=giphy.gif&ct=g" width=200 height=100/>
</div>

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

## What is the "`package.json`" ?

The [`package.json`](https://nodejs.dev/learn/the-package-json-guide) is like the official NodeJS website defines it to us :

<cite>
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.
</cite>

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 :

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

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 :

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

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`:

```json
{
    ...
    "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",
    }
    ...
}
```
<div align="center">

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

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

<a href="https://www.buymeacoffee.com/tykok"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a sushi&emoji=🍣&slug=tykok&button_colour=5F7FFF&font_colour=ffffff&font_family=Cookie&outline_colour=000000&coffee_colour=FFDD00" /></a>
</div>