Package.json Cheat Sheet
If you’ve dabbled in web development and have come across Node/NPM, you might have also come across package.json as well. As a student learning about web development, I thought I would share what I’ve learned so far through a cheat sheet series starting with package.json.
What is package.json?
The Node development environment has an awesome handy package manager called Node Package Manager otherwise known as npm. Through this package manager, you can quickly install packages for a project. The package.json file contains all the dependencies of your project. Think of the package.json file as an index of all your installed packages and metadata like the name of the project, licenses, name of the author, etc.
How do you get started with package.json?
Step 1: npm init
First thing you need to do is create the package.json file. Luckily, since npm is installed with Node all you have to do now is run npm init
through the command line. Running npm init
will prompt a series of questions to answer.
Tip: If you would like to skip the questions you can run npm init -y
to automatically answer yes to all the questions and quickly create package.json.
Step 2: npm install <insert_name_of_package_here> --save
Now that you have a package.json file, you can start adding packages by running the npm install
command followed by the name of the package. If you don’t --save,
the package will not be automatically saved in package.json. You will have to manually type it in the package.json file otherwise.
Tip: A shortcut to writing out the command is npm i -S <insert_name_of_package_here>
What’s inside the package.json file?
name
: Name of the project folder.
version
: Project version. Will default to 1.0.0.
description
: A description of the project. If left empty, the description can take information from a readme file if included.
author
: Author of the project.
contributors
: Contributors of the project.
scripts
: This is where you can add scripts.
main
: The entry file of the project.
repository:
This is where you can add the repository type and a url to the repo.
bugs
: This is where you can add a url to link to report bugs/issues.
keywords
: Can include any tags related to the project.
dependencies
: All the packages you’ve saved will be listed here like this: 'jquery':'^3.2.1'
These packages will be automatically installed when anyone else attempts to install your packages.
devDependencies
: These packages are for development and testing purposes.
license
: The project license.
What is the carat in my dependencies?
When a package is installed, you’ll notice a carat next to the version of the package like 'jquery':'^3.2.1'
. But you can also install the version of the package you want yourself by manually adding it to the package.json file and running justnpm i.
Package Version Tips
~version
Approximately equal to the version listed.
^version
Must be compatible with the version listed.
version
Must be the exact version listed.
What are scripts? How can I use them in package.json?
A script or scripting language is a computer language with a series of commands within a file that is capable of being executed without being compiled.
NPM script is a really powerful tool that you can use to help you efficiently create a project.
These are all the npm script keywords.
"scripts": {
"start": "node server.js",
"custom-script": "custom command written here"
}
- If you use one of the npm script keywords, you can run them by typing
npm <script_keyword>
in the command line. - If you are interested in making your own custom scripts, you can type in the custom name you want in
"custom-script"
and the script in the"custom command written here"
section. You can then run the custom script by typingnpm run custom-script
in the command line. - Alternatively, you can very easily write custom bash scripts and run them through npm.
Recap? Quick Tips?
Create package.json: npm init -y
Install package & save in dependencies: npm i -S <insert_name_of_package_here>
Install package & save in devDependencies: npm i -D <insert_name_of_package_here>
Install package globally: npm i <insert_name_of_package_here> -g
Uninstall package: npm r <insert_name_of_package_here>
Uninstall global package: npm r <insert_name_of_package_here> -g
Uninstall package & remove from dependencies: npm r -S <insert_name_of_package_here>
Uninstall package & remove from devDependecies:npm r -D <insert_name_of_package_here>
Run Script: node <name_of_script>
Check for outdated packages:npm outdated
Get list of installed packages: npm ls
Go to package rep0: npm repo <insert_name_of_package_here>
Go to package website: npm home <insert_name_of_package_here>
Check version of package: npm view <insert_name_of_package_here> version
Check if packages are installed in package.json: npm prune
What resources did you use?
NPM Package.json Documentation
NPM Semantic Versioner Documentation
Interactive Guide to Package.json
If I’m missing any details, please let me know! Plus if you liked what you read, please tap the 👏.