Back to Blog
Lesson 4 of the Node.js: Build Your First Server & CLI course
Node.jsJuly 15, 20264 min read

Initializing Projects with NPM: The package.json Manifest

Learn how to use npm init to create a package.json file. Understand the purpose of this manifest file and prepare your project for professional development.

node.jsnpmpackage.jsonbackenddevelopment
Motivational quote 'Make Stuff Happen' created with scrabble tiles on a clean white background.

Previously in this course, we learned how to master the REPL and run scripts from the terminal. While running individual files is fine for experiments, real-world backend applications require structure, configuration, and dependency management. That’s where npm (Node Package Manager) comes in.

In this lesson, we will initialize our REST API project and create the "manifest" file that tells Node.js exactly what your application needs to run.

What is Project Initialization?

When you build a professional application, you aren't just writing code; you are managing a product. You need to track which version of the code you're on, which external libraries (dependencies) you are using, and what commands are needed to start your server.

In the Node.js ecosystem, npm is the tool that handles this for us. By running a project initialization command, we create a package.json file. This file acts as the "source of truth" for your project. Without it, you’d have to manually track every library and configuration setting, which is a recipe for disaster in any production environment.

The Role of the package.json File

Detailed view of HTML and CSS code on a computer screen, concept of programming.

The package.json file is a JSON document that resides in the root directory of your project. It serves several critical purposes:

  1. Metadata: It stores the name, version, and description of your project.
  2. Dependency Management: It lists the exact versions of the packages your project needs to function.
  3. Scripts: It defines aliases for command-line tasks (like starting the server or running tests).

Think of it as the project’s ID card and instruction manual combined.

Worked Example: Initializing the Project

Let’s start the backend project we’ll be building throughout this course. Open your terminal, create a new directory for your project, and move into it:

Bash
mkdir my-api-project
cd my-api-project

Now, run the initialization command:

Bash
npm init

You will see a series of prompts. You can press Enter to accept the defaults for most of them, but pay attention to the entry point (usually index.js). Once you finish, npm will create a package.json file in your folder.

If you want to skip the prompts and use the default settings immediately, use the "yes" flag:

Bash
npm init -y

Your package.json will look something like this:

JSON
{
  "name": "my-api-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

Hands-on Exercise

  1. Initialize a new folder named course-project using npm init -y.
  2. Open the resulting package.json file in your code editor.
  3. Change the main field to server.js.
  4. Add a custom script under the "scripts" object called "start" that runs your file: "start": "node server.js".
  5. Run npm start in your terminal. You should see an error (because server.js doesn't exist yet), which confirms your script is working!

Common Pitfalls

  • Ignoring the node_modules folder: When you start installing packages, a folder named node_modules will appear. Never commit this to Git. It can contain thousands of files. Your package.json is enough to regenerate it.
  • Manual Editing Errors: Since it’s a JSON file, a missing comma or a trailing comma will break your configuration. Always validate your JSON if you get "unexpected token" errors.
  • Hardcoding Paths: Avoid putting absolute file paths in your package.json. Keep everything relative to the project root.

FAQ

Why does npm create a package-lock.json file? The lock file ensures that everyone working on your project uses the exact same versions of your dependencies, preventing "it works on my machine" bugs.

Can I rename my project later? Yes, just update the name field in your package.json.

What if I delete my package.json? Your project will lose its identity. You won't be able to easily install dependencies or run your scripts. Always keep it backed up in version control.

Recap

Wooden Scrabble tiles spelling 'RECAP' on a brown textured background. Text concept image.

We’ve successfully initialized our project. We now have a package.json file that acts as the manifest for our REST API. We’ve also learned how to define custom scripts, which is a standard practice for managing development workflows.

Up next, we will move beyond basic metadata and learn how to install external libraries to expand our project’s functionality.

Similar Posts