What is Package.json?

What is Package.json?

Absolute beginners guide to NPM Package.json.

·

4 min read

You're meant to encounter a file called 'package.json' one way or another, it can be quite scary if you don't know what all those elements are for so let's start from the absolute beginning!

What exactly is package.json?

You can find it in the root directory of the project and it holds relevant metadata to the project. It can contain description, version, license, scripts and many more useful information for both npm and end users.

The only two requirements (if you're planning on publishing your package) are name and version but adding more information is good practice. It must also be strictly JSON.

But, what is JSON you say?

It stands for JavaScript Object Notation which makes data-interchange easier and it's built with value pairs: name and values .

So now that we have our definitions down, let's see the structure of the package.json file.

Name and version

These are optional if you're not planning on publishing your package.

  • Name is the name you assign to your project, "js" or "node" in the name is not recommended.
  • Version is important because the end user might want to keep the package up to date with the latest version.
{
"name": "Hashnode",
"version" : "1.0.0",
}

Semantic versioning

This is the standard to make it easier to know the changes in the version of the current dependencies, libraries or frameworks you have in your project.

It goes like:

"package": "Major.Minor.Patch"

//example:
{
 "express" : "4.14.0"
}

When will those numbers increment?

  • Major: A new version completely, it won't be compatible with earlier versions.
  • Minor: Contains new features. It's compatible with earlier versions.
  • Patch: Minor security or bug fixes. It works with earlier versions too.
"express" : "~4.14.0"
//for the latest patch version we use ~


//for BOTH the Minor and Patch version we use ^
"express": "^4.14.0"

Description and Keywords

  • Description is where you briefly explain what your project does.
{
"description": "A pretty cool package.json article",
}
  • Keywords allow people to find your project and understand what it does in just a few words. It's always an array.
{
"keywords": ["blog", "hashnode", "npm"]
}

Dependencies and License

  • Dependencies help you expand your project adding external packages. When someone installs your package all the dependencies will be installed too.
"dependencies": {
 "name": "Hashnode",
"express": "4.14.0",
"mongoose": "^5.9.10"
}
  • License is to let users know what for and how they can use your project, basic ones are MIT and BSD.
{
"license": "MIT"
}

If you don't want people using your private or published package:

{
  "license": "UNLICENSED"
}

Author and Contributors

These can be used to credit the people who contributed to the project.

  • Author is one person, can have mail and url.
"author": {
"name": "YuriCodesBot",
"url" : "https://twitter.com/yuricodesbot"
}
  • Contributors is an array of people.
"contributors" : [],

Main, Scripts and Repository

  • Main defines the file used to start the project. Commonly index.js file in the root of the project, but it can be any file you choose to be the entry-point to your package.
    "main": "server.js",
  • Scripts contain the commands that are to run in the lifecycle of your package. Typically terminal commands we can reuse easily.
"scripts": {
        "start": "node server.js"
    }
  • Repository defines the url where the source code is, and what type of version it uses. This is good if people want contribute to your project.
"repository": {
        "type": "git",
        "url": "https://github.com/YuriCodes"
    }

Now let's see it all together in our package.json file:

{
    "name": "Hashnode",
    "version": "0.0.1",
    "description": "A pretty cool package.json article",
    "main": "server.js",
    "scripts": {
        "start": "node server.js"
    },
    "dependencies": {
        "express": "^4.12.4",
        "moment": "^2.14.0"
    },
    "author":{
        "name": "YuriCodesBot"
    },
    "repository": {
        "type": "git",
        "url": "https://github.com/YuriCodes"
    },
    "keywords": [
        "npm",
        "blog",
        "express",
        "hashnode"
    ],
    "license": "MIT"
}

Conclusion

I think we can both agree it's making more sense now, isn't it?

yes or no boxes, being the yes one checked

The package.json file is the heart of your project. If you want to go more in depth visit npm docs to see just how much more info you can add to it.

I hope this helps you understand better Node.js and its ecosystem!


As always thank you so much for reading! :)

I really hope you enjoyed that one.

Don't hesitate to contact me and let me know what you think in the comments!

☕If you enjoy my content Buy me a coffee It'll help me continue making quality blogs💕

💙Follow me on Twitter to know more about my self-taught journey!

💜Make sure to check my Web Design for Beginners Series and my JavaScript for Newbies Series

❤️ Also subscribe to my Youtube Channel !

🖤And for more content and tips on TikTok !

💛This is also part of the Technical Writer Journey Series!

Resources