{"id":11082,"library":"init-package-json","title":"init-package-json","description":"init-package-json is a Node.js module designed to programmatically and interactively create or update `package.json` files, primarily used by the npm CLI itself. It provides a wizard-like experience for users, prompting for essential package metadata such as name, version, description, entry point, test command, git repository, keywords, author, and license. The current stable version is 8.2.5, with frequent maintenance releases addressing bug fixes and dependency updates, often tied to releases of other `@npmcli` packages. Its key differentiator is its integration with `promzard` for flexible, scriptable prompting and the ability to utilize custom initialization files, offering a powerful way to standardize `package.json` creation across projects or teams. It is a low-level utility most often consumed by package managers rather than directly by end-user applications.","status":"active","version":"8.2.5","language":"javascript","source_language":"en","source_url":"https://github.com/npm/init-package-json","tags":["javascript","init","package.json","package","helper","wizard","wizerd","prompt","start"],"install":[{"cmd":"npm install init-package-json","lang":"bash","label":"npm"},{"cmd":"yarn add init-package-json","lang":"bash","label":"yarn"},{"cmd":"pnpm add init-package-json","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Used for interactive prompting logic during package initialization, allowing for custom wizard behavior defined in a JavaScript file.","package":"promzard","optional":false}],"imports":[{"note":"This package is primarily a CommonJS module. While Node.js versions supporting ESM are required, its main entry point is a CommonJS export.","wrong":"import init from 'init-package-json'","symbol":"init","correct":"const init = require('init-package-json')"},{"note":"The primary export is a single function, conventionally named 'init', which handles the interactive package.json creation process.","symbol":"init","correct":"const init = require('init-package-json')"},{"note":"When using a custom initFile, the `configData` and `package` objects are passed into the `promzard` module's context, accessible as `config` and `package` variables within that file.","symbol":"PromZardContext","correct":"/* PromZard context is implicitly passed. See PromZard documentation. */"}],"quickstart":{"code":"const init = require('init-package-json');\nconst path = require('path');\nconst fs = require('fs/promises');\n\nasync function initializePackage() {\n  // Define a temporary custom init file for demonstration purposes\n  const tempInitFileContent = `\n    module.exports = function (data, cb) {\n      data.name = this.basename;\n      data.version = '1.0.0';\n      data.description = 'A new package initialized via init-package-json!';\n      data.main = 'index.js';\n      data.scripts = { test: 'echo \\\"Error: no test specified\\\" && exit 1' };\n      data.license = 'ISC';\n      data.author = this.config.author || '';\n      cb(null, data);\n    };\n  `;\n  const customInitFilePath = path.join(__dirname, '.npm-init-custom.js');\n  await fs.writeFile(customInitFilePath, tempInitFileContent);\n\n  const targetDir = path.join(__dirname, 'my-new-package');\n  await fs.mkdir(targetDir, { recursive: true });\n\n  const configData = { author: 'My Name <me@example.com>' };\n\n  try {\n    console.log(`Initializing package.json in ${targetDir}...`);\n    const resultData = await init(targetDir, customInitFilePath, configData);\n    console.log('package.json initialized successfully:');\n    console.log(JSON.stringify(resultData, null, 2));\n  } catch (error) {\n    console.error('Failed to initialize package.json:', error);\n  } finally {\n    // Clean up the temporary init file\n    await fs.unlink(customInitFilePath);\n    console.log('Cleaned up temporary init file.');\n  }\n}\n\ninitializePackage();","lang":"javascript","description":"This quickstart demonstrates how to programmatically use `init-package-json` to create a `package.json` file in a specified directory, utilizing a custom `promzard` init file to pre-fill common fields and interact with passed configuration data."},"warnings":[{"fix":"Ensure your Node.js environment meets the specified version requirements. Update Node.js to a compatible version if necessary.","message":"Version 8.0.0 introduced significant changes to Node.js compatibility requirements. The package now requires Node.js `^20.17.0 || >=22.9.0`.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"Be aware of the new `type` prompt if using interactive mode. If you rely on a specific field order, be prepared for changes in generated `package.json` files.","message":"In v8.0.0, a new `type` prompt was added during the interactive initialization process, and the sort order of fields in the created `package.json` file was changed.","severity":"breaking","affected_versions":">=8.0.0"},{"fix":"If providing a custom `initFile`, ensure it's a valid `promzard` script that exports a function with the expected signature `(data, cb) => {}` and utilizes the `this.config` and `this.package` context for data manipulation.","message":"The `initFile` parameter expects a path to a `promzard` module, which is a JavaScript file exporting a function, not a simple JSON configuration file. This allows for dynamic and interactive prompting logic.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Use `const init = require('init-package-json')` for CommonJS projects. If in an ESM module, consider `await import('init-package-json')` if the package provides a compatible export, or wrap it in a CommonJS context.","cause":"Attempting to `import` `init-package-json` as an ES module in a CommonJS context, or vice-versa, when the package is primarily CommonJS.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module <path/to/module> not supported. Instead change the require of <path/to/module> to a dynamic import() which is available in all CommonJS modules."},{"fix":"Upgrade your Node.js environment to a version compatible with `^20.17.0` or `>=22.9.0`.","cause":"Running the package with an unsupported Node.js version.","error":"Error: The \"init-package-json\" package requires Node.js version ^20.17.0 || >=22.9.0."},{"fix":"Double-check the `initFile` path to ensure it is absolute and points to an existing JavaScript file that exports a `promzard` function.","cause":"The `initFile` path provided to the `init` function is incorrect, or the file does not exist.","error":"Error: Cannot find module '<path/to/custom/initFile>'"}],"ecosystem":"npm"}