开发npm SDK时如何获取package.json版本且避免完整打包?
Great question! I’ve dealt with this exact problem when building npm SDKs—pulling in the full package.json into your build output is messy, especially if it contains extra metadata or sensitive config you don’t want exposed. Here are three practical, clean solutions to avoid that:
1. Inject Version via Build-Time Replacement (Works with Rollup/Webpack/Vite)
Most modern build tools let you inject static values directly into your code during the build process, which means you can pull the version from package.json and hardcode it into your SDK without bundling the whole file.
Example with Rollup:
Use @rollup/plugin-replace to inject the version as a constant:
// rollup.config.js import replace from '@rollup/plugin-replace'; import pkg from './package.json'; export default { plugins: [ replace({ preventAssignment: true, __SDK_VERSION__: JSON.stringify(pkg.version), }), ], };
Then in your SDK code:
// src/index.js export const version = __SDK_VERSION__;
Example with Webpack:
Use DefinePlugin for similar behavior:
// webpack.config.js const webpack = require('webpack'); const pkg = require('./package.json'); module.exports = { plugins: [ new webpack.DefinePlugin({ __SDK_VERSION__: JSON.stringify(pkg.version), }), ], };
Example with Vite:
Vite automatically injects import.meta.env.npm_package_version for you—no extra plugins needed:
// src/index.js export const version = import.meta.env.npm_package_version;
2. Use a Dedicated Version File (Build-Tool Agnostic)
If you want a solution that doesn’t depend on your build setup, create a standalone file to hold the version, then sync it with package.json using an npm script.
- Create
src/version.js:
export const version = '1.0.0';
- Add a
versionscript to yourpackage.jsonthat updates this file whenever you bump the version:
{ "scripts": { "version": "node -e \"fs.writeFileSync('./src/version.js', 'export const version = \\'' + require('./package.json').version + '\\';')\" && git add src/version.js" } }
Now, when you run npm version patch (or minor/major), npm will automatically update src/version.js to match the new package.json version, then stage the file for git. You can safely import the version in your SDK without touching package.json at all.
3. Use process.env.npm_package_version (Node.js Only SDKs)
If your SDK is intended only for Node.js environments (not browsers), you can directly access the version via Node’s built-in environment variables without any build steps:
export const version = process.env.npm_package_version;
This works because Node automatically injects npm package metadata into process.env when your package is installed or run via npm scripts. Note that this won’t work in browser builds, since process isn’t available there.
All these approaches avoid bundling the entire package.json while keeping your SDK version in sync with the one in package.json. My top pick depends on your stack: use build-time replacement if you’re using a modern bundler, or the dedicated version file if you want maximum compatibility.
内容的提问来源于stack exchange,提问作者Tnargib

