Projects created with Node generally have lots of packages. These packages are also often called libraries or dependencies.
Lucky for us, Node provides us with sufficient infrastructure to manage our dependencies out of the box. This infrastructure is called npm and it uses a package.json file.
Creating a project
First, you need to create a folder to house your project. You can call it anything you want.
Initializing npm
Next, you want to navigate to this project with your Terminal.
When you’re inside the project, run npm’s init command. This command creates a package.json that holds information about dependencies.
npm initWhen you run npm init, the Terminal will prompt you with lots of questions.
These questions are only relevant if you’re writing a package that you want others to consume. We’re not doing that so we can ignore all of these questions and just hit enter for every question that pops up.
When the process is complete, npm will create a package.json file. Here’s an example of what package.json contains.
You don’t have know most of values at the moment. We’ll cover everything you need to know to make a server in this course.
Saving Dependencies
When you download a library, you can save it into the package.json file with a -S flag or a -D flag.
-Sor--save: Save as a dependency-Dor--save-dev: Save as a dev dependency
Here’s what they mean:
- Dependencies are used to indicate packages that are critical for the server to work.
- Dev dependencies are used to indicate packages that are used in development mode.
Don’t worry about the differences for now. We’ll talk about the difference between dependencies and dev dependencies again as we install some packages over the next few lessons.
Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson