{"id":20526273,"url":"https://github.com/lostpebble/generate-package-json-webpack-plugin","last_synced_at":"2025-07-21T23:32:14.885Z","repository":{"id":44792629,"uuid":"96036143","full_name":"lostpebble/generate-package-json-webpack-plugin","owner":"lostpebble","description":"Generates a package.json file containing the external modules used by your webpack bundle","archived":false,"fork":false,"pushed_at":"2023-03-14T17:25:07.000Z","size":149,"stargazers_count":71,"open_issues_count":8,"forks_count":18,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-20T06:50:55.972Z","etag":null,"topics":["nodejs","nodejs-modules","package-json","webpack","webpack-plugin"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/generate-package-json-webpack-plugin","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lostpebble.png","metadata":{"files":{"readme":"README.md","changelog":"changelog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-02T16:37:14.000Z","updated_at":"2025-04-05T18:42:00.000Z","dependencies_parsed_at":"2024-06-18T13:58:37.932Z","dependency_job_id":"35953b2c-d3f4-447b-8c52-bf1b3a7b0100","html_url":"https://github.com/lostpebble/generate-package-json-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"purl":"pkg:github/lostpebble/generate-package-json-webpack-plugin","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostpebble%2Fgenerate-package-json-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostpebble%2Fgenerate-package-json-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostpebble%2Fgenerate-package-json-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostpebble%2Fgenerate-package-json-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lostpebble","download_url":"https://codeload.github.com/lostpebble/generate-package-json-webpack-plugin/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lostpebble%2Fgenerate-package-json-webpack-plugin/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266403053,"owners_count":23923403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["nodejs","nodejs-modules","package-json","webpack","webpack-plugin"],"created_at":"2024-11-15T23:13:27.787Z","updated_at":"2025-07-21T23:32:14.782Z","avatar_url":"https://github.com/lostpebble.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# generate-package-json-webpack-plugin\n\n\u003e For limiting the dependencies inside `package.json` to only those that are actually being used by your code.\n\n### Why generate a new `package.json`?\n\nThis plugin is useful for when you have a large source project for development / testing from which smaller Node.js\nprojects are bundled for various deployments and applications. Such as Google Cloud Functions.\n\n_Or even just for bundling your regular Node.js server code, and knowing that your `package.json` is as lean as it can\npossibly be for that next deployment._\n\nWe all know how our development environments can get a bit messy... :sweat_smile:\n\n### :floppy_disk: Install\n\n```\nnpm install generate-package-json-webpack-plugin --save-dev\n```\n\n## :electric_plug: Usage\n\n```\nconst basePackage = {\n  \"name\": \"my-nodejs-module\",\n  \"version\": \"1.0.0\",\n  \"main\": \"./index.js\",\n  \"engines\": {\n    \"node\": \"\u003e= 14\"\n  }\n}\n\n// inside your webpack configuration\nplugins: [new GeneratePackageJsonPlugin(basePackage)],\n```\n\nThat's pretty much it. The plugin will generate a new `package.json` file with all the dependencies your code uses. The\nversions for the detected dependencies are sourced directly from the modules inside `node_modules`.\n\n**N.B.** This base package file is deliberately barren, as a base to build upon for our final output `package.json`- any\ndependencies listed inside of it will be set deliberately and interpretted differently by the generation process. [See\nbelow for more information](#adding-modules-outside-of-your-code-build-modules-etc).\n\n### Important note on `externals`\n\nThe plugin only writes the dependencies of modules which are found in the input code and have been marked in `externals`\ninside of your Webpack config.\n\nThis is logical because if a module is not marked as an external module it is included in your final webpack bundle and\nhence wouldn't need to be installed as a dependency again on deployment.\n\nBecause of this, this plugin is best used in conjunction with something\nlike [webpack-node-externals](https://github.com/liady/webpack-node-externals), which you can use to make sure your node\nmodules are not included with your final `bundle.js`, like so:\n\n```\nconst nodeExternals = require(\"webpack-node-externals\");\n\n// inside your webpack config\nexternals: [nodeExternals({\n    whitelist: [/^module-I-want-bundled/],\n})],\n```\n\nAs you can see, you can add modules that you deliberately _do_ want bundled using the `whitelist` option.\n\n### Adding modules outside of your code (build modules etc.)\n\n#### OR to deliberately set different versions of bundled dependencies, or as different dependency type (`peerDependencies`, for example)\n\nSimply place those dependencies inside the `basePackageValues` object which represents the base of the\nnew `package.json` to be created.\n\nKeep the version number string empty (`\"\"`) to have the plugin resolve the version. To use a version which is\ndifferent, set the version string deliberately here.\n\n```\nconst basePackageValues = {\n  \"name\": \"my-nodejs-module\",\n  \"version\": \"1.0.0\",\n  \"main\": \"./index.js\",\n  \"scripts\": {\n    \"start\": \"cross-var node --max-old-space-size=$NODE_JS_MAX_OLD_SPACE_SIZE ./server.js\"\n  }\n  \"engines\": {\n    \"node\": \"\u003e= 14\"\n  },\n  devDependencies: {\n    \"cross-var\": \"^1.1.0\",\n    \"cross-env\": \"\",\n  },\n  peerDependencies: {\n    \"react\" : \"\",\n  }\n}\n```\n\nIn this example, `cross-var` has deliberately been set to version `^1.1.0`, and regardless of what is actually installed\nit will use this version.\n`cross-env` however will pull its version number from `node_modules`.\n\nThis is mostly useful for adding dependencies which are required at runtime but which are not picked up in your webpack\nbundle. Such as `cross-var` in this example which injects environment variables into a run script in a cross-platform\nfriendly way.\n\nNote that the same behaviour applies to all types of dependencies (`dependencies`, `devDependencies` and\n`peerDependencies`). In this example `react` will have the same behaviour as `cross-env`, but rather than being placed\ninside the `dependencies` list in the output file, it will be placed inside the `peerDependencies` list.\n\n## Simple API\n\n```\nnew GeneratePackageJsonPlugin(basePackage, options)\n```\n\n### First argument: `basePackage`\n\n( **Required** ) You should set the base values for your `package.json` file here. For example:\n\n```\nconst basePackage = {\n  \"name\": \"my-nodejs-module\",\n  \"version\": \"1.0.0\",\n  \"main\": \"./bundle.js\",\n  \"engines\": {\n    \"node\": \"\u003e= 14\"\n  }\n}\n```\n\nThis will be merged with the generated `\"dependencies\": { ... }` to form the final `package.json` distribution file.\n\n### Second argument: `options`\n\n( *Optional* )\n\nAn object with the following structure:\n\n```\n  {\n     debug: true,\n     useInstalledVersions: true,\n     resolveContextPaths: [__dirname],\n     sourcePackageFilenames: [\n       join(__dirname, \"../other-workspace/package.json\"),\n     ],\n     forceWebpackVersion: \"webpack5\",\n     excludeDependencies: [\"aws-sdk\"],\n  }\n```\n\n**The options:**\n\n`debug` (default: *false*) : Enable to show some debugging information on how the plugin is finding dependencies and\ncreating a new `package.json`.\n\n`useInstalledVersions` (default: *true*) : Resolve node modules and use the exact version that is installed in your\nenvironment. This is useful to lock versions on production deployments. This is the default and easiest way to use the\nplugin, if this is not enabled then you should be providing package.json files in `sourcePackageFilenames` from which\nthe plugin will source module versions.\n\n`resolveContextPaths`: Context paths for the internal resolve behaviour that looks upwards for `node_modules` to pull\nthe versions from. The current directory is the default, but if you have a monorepo, there are edge cases where defining\nmultiple contexts could be useful.\n\n`sourcePackageFilenames` : If the default `useInstalledVersions` option is set, then this is only used as a final\nfallback for finding versions. This is useful for mono-repos and projects where your dependencies in your code are not\nonly defined from a single contextual project. If you share code between multiple projects or \"workspaces\" to be bundled\ninto a final distribution project, you might want to set this option.\n\n`forceWebpackVersion` (optional- by default the plugin will attempt to detect the version) : This can be set to one of: \n`webpack4` or `webpack5`. If you are using a version of Webpack lower than 4- then set it to `webpack4`. This may help\nfolks who are using Webpack in an environment where multiple versions might be present.\n\n`excludeDependencies` : Here you can set any dependencies you absolutely never want in your output `package.json` file, \neven if they happen to be used by your code. This is useful in some edge cases, such as where an execution environment\nprovides these dependencies for you automatically, without installation required.\n\n### :mag: Things to take note of\n\nYou should remember to set the `\"main\": \"./index.js\"` to the correct filename (would probably be the output bundle file\nfrom the same webpack task), and / or correctly set your starting script which will be run on Node.js server deployments\nby `npm start`. You can set these values in the `basePackage` object you pass into the plugin, example:\n\n```\nconst basePackage = {\n  \"name\": \"my-nodejs-module\",\n  \"version\": \"1.0.0\",\n  \"main\": \"./bundle.js\",\n  \"scripts\": {\n    \"start\": \"node ./bundle.js\"\n  },\n  \"engines\": {\n    \"node\": \"\u003e= 14\"\n  }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flostpebble%2Fgenerate-package-json-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flostpebble%2Fgenerate-package-json-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flostpebble%2Fgenerate-package-json-webpack-plugin/lists"}