generate-package-json-webpack-plugin

raw JSON →
2.7.0 verified Sat Apr 25 auth: no javascript

A Webpack plugin that generates a minimal package.json containing only the external dependencies actually used by your bundle. Version 2.7.0 is current, with regular updates. It works with Webpack 4 and 5, and is typically paired with webpack-node-externals. Key differentiators: automatic version resolution from node_modules, support for excluding dependencies, forcing a Webpack version in mixed environments (e.g., Nx), and flexibility to add non-code dependencies with explicit version prefixes. Ships TypeScript types.

error TypeError: GeneratePackageJsonPlugin is not a constructor
cause Importing the plugin with named import syntax instead of default import.
fix
Use import GeneratePackageJsonPlugin from 'generate-package-json-webpack-plugin' or const GeneratePackageJsonPlugin = require('generate-package-json-webpack-plugin').
error Error: Cannot find module 'webpack'
cause Webpack is not installed as a peer dependency.
fix
Run npm install webpack --save-dev.
error Error: No output package.json generated
cause No external modules were used, or all modules are bundled (not externalized).
fix
Ensure you have externalized node_modules using webpack-node-externals or similar, and that your code imports at least one external module.
error Error: version not found for module 'some-module'
cause The module is installed but the plugin cannot find its version, possibly due to a resolver issue in newer Node versions.
fix
Update to plugin version >=2.3.0 or ensure the module's package.json is accessible.
breaking In v2.0.0, the API was simplified: only two parameters (basePackage and options) are accepted. Passing basePackageValues as second argument is deprecated.
fix Update to use options object: new GeneratePackageJsonPlugin(basePackage, { useInstalledVersions: true, ... })
breaking In v2.0.0, useInstalledVersions default changed from false to true. If you relied on versions from a separate package.json, you must now explicitly set useInstalledVersions: false.
fix Set options.useInstalledVersions: false if you want to use a separate versions package.json file.
deprecated In v2.0.0, the old way of specifying versionsPackageFiles as a separate argument is removed. Use versionsPackageFiles inside the options object.
fix new GeneratePackageJsonPlugin(basePackage, { versionsPackageFiles: ['path/to/versions.json'] })
gotcha The plugin only includes dependencies that are marked as externals in the Webpack config. If a module is not external, it won't appear in the generated package.json.
fix Use webpack-node-externals or manually externalize node_modules.
gotcha When using Webpack 5, the plugin may incorrectly detect the Webpack version in mixed environments (e.g., Nx). Versions 2.2.0+ have a forceWebpackVersion option to resolve this.
fix Set forceWebpackVersion: 'webpack4' or 'webpack5' in options.
npm install generate-package-json-webpack-plugin
yarn add generate-package-json-webpack-plugin
pnpm add generate-package-json-webpack-plugin

Minimal Webpack config using the plugin with webpack-node-externals and an excluded dependency.

import GeneratePackageJsonPlugin from 'generate-package-json-webpack-plugin';
import nodeExternals from 'webpack-node-externals';

const basePackage = {
  name: 'my-app',
  version: '1.0.0',
  main: 'dist/index.js',
  engines: { node: '>= 14' }
};

export default {
  target: 'node',
  entry: './src/index.js',
  output: { path: './dist', filename: 'index.js' },
  externals: [nodeExternals()],
  plugins: [
    new GeneratePackageJsonPlugin(basePackage, {
      excludeDependencies: ['aws-sdk']
    })
  ]
};