Initialize a Node.js package
Low risk
What do risk levels mean?
- Read-only
- Inspects state without changing anything.
- Low risk
- Reversible with a routine follow-up command.
- Medium risk
- Changes state; undo path documented.
- Destructive
- Deletes or overwrites; confirmation required.
Checks for existing package metadata, then creates a package.json with npm defaults so the directory can manage scripts and dependencies.
nodenpmpackage
Commands
Check whether package metadata already exists before initialization
node -e "console.log(require('node:fs').existsSync('package.json') ? 'package.json exists' : 'package.json does not exist')"Create package.json using npm defaults
npm init --yesVerification
npm pkg get name versionnpm prints the new package name and version from package.json.
Undo
Not undoable
Existing package metadata cannot be reconstructed automatically; remove a newly created package.json only after confirming it did not exist before initialization.
Pitfalls
- npm can reuse fields from an existing package.json; commit or back it up before initialization if preserving its exact contents matters.
- Review and edit generated metadata such as name, version, and license before publishing.