package.json generator: scripts, type and engines
Create a package.json with the right name, version, module type, scripts and Node version.
package.json describes your Node.js project: its name, how to start it, and which packages it needs. Fill in the basics to get a clean starting file, then add packages with npm install.
Create your config file
Type your own values. The file below updates as you type, and nothing you type leaves your browser.
{
"name": "my-app",
"version": "1.0.0",
"description": "My Node.js app",
"main": "index.js",
"type": "commonjs",
"private": true,
"scripts": {
"start": "node index.js",
"dev": "node --watch index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"engines": {
"node": ">=20"
},
"license": "MIT"
}How this setup works
Step-by-step setup
- Create a folder for your project and save the file above in it as package.json.
- Create the main file index.js with some code, such as console.log("Hello").
- Install a package: npm install express. This adds a dependencies section and a package-lock.json file.
- Run the app: npm start.
- Commit package.json and package-lock.json to Git. Do not commit node_modules.
What the important lines mean
- name and version identify the package. Names must be lowercase with no spaces.
- "type": "module" lets you use import and export. Leave it as commonjs to use require.
- scripts are commands you run with npm run name. start and test also work as npm start and npm test.
- engines states the Node versions the project supports.
Common errors and how to fix them
- npm error code EJSONPARSE
- package.json is not valid JSON, often a trailing comma or a missing quote. Paste it into a JSON checker to find the line.
- npm warn config … Invalid name
- The name has capital letters or spaces. Use lowercase letters, numbers and hyphens.
- SyntaxError: Cannot use import statement outside a module
- You used import but the type is commonjs. Set the module type to ES modules, or use require.
- ReferenceError: require is not defined in ES module scope
- The type is module, so require is not available. Use import, or switch to CommonJS.
- npm error Missing script: "start"
- Tick the start script above, or add it under "scripts".
Frequently asked questions
- What does npm init do?
- It asks you questions and writes a package.json. This generator gives you the same file without the questions.
- Should I commit package-lock.json?
- Yes. It locks the exact package versions so everyone, and your server, installs the same ones.
- What is the difference between dependencies and devDependencies?
- dependencies are needed to run the app. devDependencies are only for development, such as test tools. Add them with npm install --save-dev.
Fix a problem with this setup
- npm EACCES permission deniedFix npm "EACCES: permission denied, mkdir /usr/local/lib/node_modules" without sudo: change the npm folder, use nvm or fix ownership.
- Node EADDRINUSEFix EADDRINUSE in Node, Express, Next.js and Vite: find the program using the port, stop it, or start on another port.
More developer configs
- tsconfig.json fileCreate a tsconfig.json for a Node or browser TypeScript project, with strict mode, target and output folder.
- SSH config for multiple GitHub accountsUse two or three GitHub accounts on one computer with separate SSH keys and host names.
- SSH config fileGive servers short names and choose the right key for each.
- .gitconfig setupSet your name, email, default branch and shortcuts once.
- .env file exampleCreate a .env file for your app: port, database address and a secret key.
- VS Code settings.jsonSet font, tab size, format on save and theme in your VS Code settings.