This guide explains the complete process of initializing a backend project and editing the package.json file for beginners.
- Open your terminal or command prompt.
- Navigate to the backend folder:
cd <your folder path>
- If the folder does not exist, create it first, then navigate into it.
- Run the npm initialization command:
npm init -y
- This creates a
package.jsonfile with default values. - If you want to customize values, run:
and answer the prompts.
npm init
After initialization, package.json typically contains:
name: the project nameversion: the project versiondescription: a short descriptionmain: the entry file (usuallyindex.js)scripts: commands you can run withnpm runauthor: your name or organizationlicense: the project license
Open package.json in a code editor and update these sections:
name: use a lowercase, hyphenated name.version: keep as1.0.0for a new project.description: write a short description.main: set the entry point, for exampleserver.jsorapp.js.scripts: add useful commands:"scripts": { "start": "node server.js", "dev": "nodemon server.js" }
dependencies: will list runtime packages likeexpress,mongoose, etc.devDependencies: will list development tools likenodemon.
Example package.json structure:
{
"name": "backend",
"version": "1.0.0",
"description": "MERN backend project",
"main": "server.js",
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
},
"author": "Your Name",
"license": "MIT"
}- Install Express:
npm install express
- Install development tools like nodemon:
npm install --save-dev nodemon
- Install other packages as needed, e.g.:
npm install mongoose dotenv
- Start the server:
npm start
- Start in development mode with auto-reload:
npm run dev
- After editing
package.json, save the file. - Confirm the file is valid JSON by checking commas, braces, and quotes.
- Run
npm installif you add new dependencies.
- Initialize the backend folder with
npm init -y. - Edit
package.jsonto set the entry file and scripts. - Install needed packages with
npm install. - Use
npm startornpm run devto run the backend.