What is package.json
When we create or install node packages, we need a way to essentially, define the package. Things like the author, version number, description, package dependencies and much more. This is where the package.json file comes in. It is required for every package and is always included in the root folder of the package.
Creating a package.json file
When we create a new node package, we create a "parent" folder to hold all of our package files. This is where our package.json file will reside. To demonstrate our principles, we are going to utilize a package called figlet, which creates ASCII art. The package can be found in the Node Registry, .
We're going to start by creating our "parent" folder [figlet]. Now we need to open up our [node] Terminal and navigate to this folder. Once inside the empty [figlet] folder, and from within the [node terminal], we run [npm init].
This will ask us a series of questions designed to help us build a basic package.json file. Now we could create this file entirely on our own, but the init method simplifies the process and ensures that we have some of the most basic info. Just answer the questions as they come up. If any particular question does not apply to your package, just press [enter] and move on. It is not required to answer every question.
Once we finish the process we have a figlet folder with one file, [package.json]. And just for clarity, here is the contents of the file created:
- json
- package.json
- {
- "name": "figlet",
- "version": "1.0.0",
- "description": "A package that generates ASCII Art",
- "main": "index.js",
- "scripts": {
- "test": "echo \"Error: no test specified\" && exit 1"
- },
- "keywords": [
- "figlet"
- ],
- "contributors": [
- {
- "name": "patorjk"
- },
- {
- "name": "badDoggy"
- }
- {
- ],
- "license": "ISC"
- }
(*note: We can just use [npm init -y] if we want to fill out the info in the package.json file later. The -y flag skips all of the questions, and makes your package.json file with empty values.)
Now that we have our basic package.json file, now we need to install our [figlet] package. We should already be in our figlet directory we created, but if not, just navigate to it in the terminal. Then type [npm i figlet], as shown in the package documentation.
After the figlet install, if we look at our package.json file we notice that it has add the following "dependencies" to our project:
- "dependencies": {
- "figlet": "^1.8.0"
- }
We should now be able to write the code to run our figlet project. Remember that an [index.js] file is required in the root project folder, so we're going to create that now. In this file we will require the figlet package we installed, and set a variable for the text we want to use for the art:
- JavaScript
- index.js
- // import the figlet package
- const figlet = require('figlet');
- // Set the text for the art
- const artText = 'Hello World!!';
And now we can add some code to our [index.js] file, so we can console.log some ASCII Art. We'll just copy the example from the figlet website.
- JavaScript
- index.js
- figlet( artText, function ( err, data ) {
- if (err) {
- console.log("Something went wrong...");
- console.dir(err);
- return;
- }
- console.log(data);
- if (err) {
- });
and if we run [index.js] the console should log:
_ _ _ _ __ __ _ _ _ _ | | | | ___| | | ___ \ \ / /__ _ __| | __| | | | | |_| |/ _ \ | |/ _ \ \ \ /\ / / _ \| '__| |/ _` | | | | _ | __/ | | (_) | \ V V / (_) | | | | (_| |_|_| |_| |_|\___|_|_|\___/ \_/\_/ \___/|_| |_|\__,_(_|_)