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 manifes…


This content originally appeared on DEV Community and was authored by Tykok

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",
    }
    ...
}

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",
    }
    ...
}

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",
    }
    ...
}

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

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


This content originally appeared on DEV Community and was authored by Tykok


Print Share Comment Cite Upload Translate Updates
APA

Tykok | Sciencx (2022-05-11T17:36:14+00:00) Use variables in your package.json. Retrieved from https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/

MLA
" » Use variables in your package.json." Tykok | Sciencx - Wednesday May 11, 2022, https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/
HARVARD
Tykok | Sciencx Wednesday May 11, 2022 » Use variables in your package.json., viewed ,<https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/>
VANCOUVER
Tykok | Sciencx - » Use variables in your package.json. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/
CHICAGO
" » Use variables in your package.json." Tykok | Sciencx - Accessed . https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/
IEEE
" » Use variables in your package.json." Tykok | Sciencx [Online]. Available: https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/. [Accessed: ]
rf:citation
» Use variables in your package.json | Tykok | Sciencx | https://www.scien.cx/2022/05/11/use-variables-in-your-package-json/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.