rollup-plugin-consts

raw JSON →
1.2.0 verified Mon Apr 27 auth: no javascript maintenance

Import build-time constants (e.g., NODE_ENV, version) as ES module imports in Rollup. Unlike string-replacement plugins (rollup-plugin-replace), it uses a virtual module prefix 'consts:' for cleaner code and TypeScript support. Latest version 1.2.0, stable and unmaintained since 2020. Ships with TypeScript types. Peer dep rollup >=1.15.0 <4. ESM-only, no CommonJS.

error Error: Cannot find module 'consts:environment'
cause The plugin is not registered in the rollup config plugins array or is placed after other plugins that cache resolution.
fix
Ensure consts() is called in the rollup plugins array and that it appears early (before plugins like commonjs).
error [!] TypeError: consts is not a function
cause Using a named import instead of default import, or using CommonJS require without .default.
fix
Use the default import: import consts from 'rollup-plugin-consts';
error RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)
cause The constant value contains a non-JSON literal (e.g., a function) that Rollup cannot parse as JavaScript.
fix
Pass only strings, numbers, booleans, arrays, or plain objects as constant values.
breaking rollup-plugin-consts is incompatible with Rollup 4+. Peer dependency specifies rollup >=1.15.0 <4.
fix Use an alternative like @rollup/plugin-replace or migrate to a newer plugin.
deprecated Package has not been updated since 2020. May not receive fixes for future Rollup versions.
fix Consider using @rollup/plugin-replace or rollup-plugin-swc with define.
gotcha Constants are evaluated at build time and must be serializable (strings, booleans, objects). Functions or complex expressions are not supported.
fix Pass only plain JSON-serializable values as constants.
gotcha Virtual modules use the 'consts:' prefix and must be explicitly listed in TypeScript declarations.
fix Create a .d.ts file, e.g., declare module 'consts:*' { const value: any; export default value; }
npm install rollup-plugin-consts
yarn add rollup-plugin-consts
pnpm add rollup-plugin-consts

Shows how to set up the plugin in rollup.config.js and use imported constants from virtual modules.

// rollup.config.js
import consts from 'rollup-plugin-consts';

export default {
  input: 'src/index.js',
  output: { file: 'dist/bundle.js', format: 'es' },
  plugins: [
    consts({
      environment: 'production',
      version: '1.0.0',
      config: { names: ['foo', 'bar'] }
    })
  ]
};

// src/index.js
import environment from 'consts:environment';
import version from 'consts:version';
import config from 'consts:config';

console.log(environment, version, config.names);