rollup-plugin-generate-package-json

raw JSON →
3.2.0 verified Mon Apr 27 auth: no javascript

Rollup plugin that generates a lean package.json file containing only the dependencies from your bundle, optionally supplemented with additional dependencies. Version 3.2.0, released 2021-01-28 with active maintenance; supports Rollup >=1.0.0 and Node >=8.3. Key differentiator: automatically extracts dependencies used in the bundle, avoiding manual pruning of package.json for deployment. Offers options to customize base contents (via object or function) and add extra dependencies with version overrides. ESM-only plugin.

error Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /path/node_modules/rollup-plugin-generate-package-json/dist/index.js
cause Using CommonJS require() to load an ESM-only plugin.
fix
Use the import statement: import generatePackageJson from 'rollup-plugin-generate-package-json'
error TypeError: generatePackageJson is not a function
cause Using named import instead of default import.
fix
Use default import: import generatePackageJson from 'rollup-plugin-generate-package-json'
error Error: [generate-package-json] Input package.json not found at specified folder: /path/to/folder
cause The 'inputFolder' option points to a folder that does not contain a package.json file.
fix
Ensure the folder exists and contains a valid package.json, or set 'inputFolder' to the correct path.
error Error: [generate-package-json] Output folder is not defined. Use 'outputFolder' option or use 'dir' for rollup output.
cause Neither 'outputFolder' option nor Rollup's 'output.dir' is set.
fix
Set the 'outputFolder' option in the plugin or provide 'dir' in Rollup output configuration.
breaking In v3.0.0, the option 'inputPackageJson' was renamed to 'inputFolder' and its semantic changed: it now points to the folder containing the input package.json, not the file itself.
fix Replace 'inputPackageJson' option with 'inputFolder' set to the folder path (e.g., 'src').
breaking In v2.0.0, the option 'inputFile' was renamed to 'inputPackageJson'.
fix Rename 'inputFile' to 'inputPackageJson'.
deprecated Support for Rollup < 1.0.0 is dropped; peer dependency rollup >= 1.0.0 required.
fix Upgrade Rollup to version 1.0.0 or higher.
breaking Node.js version < 8.3 is no longer supported in v3.2.0.
fix Update Node.js to version 8.3 or higher.
gotcha The plugin generates package.json in the output folder determined by Rollup's 'dir' option (or 'file' parent). Ensure 'output.dir' is set appropriately.
fix Use 'output.dir' in Rollup config, not 'output.file' (or set 'outputFolder' explicitly to override).
npm install rollup-plugin-generate-package-json
yarn add rollup-plugin-generate-package-json
pnpm add rollup-plugin-generate-package-json

Shows basic usage: import plugin, configure output dir, set base package.json contents and extra dependencies.

// rollup.config.js
import generatePackageJson from 'rollup-plugin-generate-package-json';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'cjs'
  },
  plugins: [
    generatePackageJson({
      baseContents: {
        name: 'my-app',
        private: true,
        scripts: { start: 'node index.js' }
      },
      additionalDependencies: ['pg']
    })
  ]
};