How to configure package.json for a JavaScript project

· Category: JavaScript

Short answer

package.json defines project metadata, dependencies, scripts, and configuration. Key fields include name, version, main, type, scripts, and dependencies.

Steps

  1. Initialize a project: bash npm init -y
  2. Define scripts: json { "scripts": { "start": "node src/index.js", "build": "webpack --mode=production", "test": "jest" } }
  3. Set the module type: json { "type": "module" }
  4. Specify entry points: json { "main": "dist/index.cjs", "module": "dist/index.mjs" }

Tips

  • Use engines to specify supported Node.js versions.
  • Use files to whitelist what gets published to npm.

Common issues

  • Forgetting to set type: module when using ES modules causes syntax errors.
  • Misconfigured main fields lead to consumers importing the wrong file.