PortfolioHire me for one like this

Sample website by Chayangkoon

CodeSprout

Lesson 2 / 6

Lesson 2

npm and packages

7 min read

When you install Node.js you also get npm, the Node Package Manager. A package is a folder of code someone else wrote and published so you can reuse it. Next.js is a package. So are React and thousands of smaller helpers for dates, forms and animation.

Starting a project

The quickest way to start a Next.js project is the official starter. It asks a few questions, creates a folder, and installs everything for you.

npx create-next-app@latest my-site
cd my-site
npm run dev
# Open http://localhost:3000 in your browser

npx runs a package without installing it permanently. Here it runs create-next-app once to set up the project. After that, npm run dev starts the development server you met in lesson 1.

package.json: the project's shopping list

Every Node.js project has a package.json file at its root. It records the project's name, the packages it depends on, and short commands called scripts.

package.json
{
  "name": "my-site",
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start"
  },
  "dependencies": {
    "next": "16.4.2",
    "react": "19.3.0",
    "react-dom": "19.3.0"
  }
}

The dependencies section is a shopping list. When you run npm install, npm reads that list, downloads each package, and puts it in a folder called node_modules. It also writes package-lock.json, which pins the exact versions so everyone on the team gets the same code.

Scripts you will use every day

  • npm run dev starts the development server with instant reloads.
  • npm run build makes an optimised production version of the site.
  • npm run start runs that production version on a Node.js server.
  • npm install <name> adds a new package and records it in package.json.

A script is just a nickname for a longer command. npm run dev really runs next dev, a command provided by the Next.js package inside node_modules. You can add your own scripts too, for example one that runs your tests.

Before you install a package, check that it is actively maintained and widely used. Every package you add is code that runs in your project, so fewer, well-known packages make a site easier to look after. With the project running, you are ready to make your first page.