{"id":21961,"library":"rollup-plugin-consts","title":"rollup-plugin-consts","description":"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.","status":"maintenance","version":"1.2.0","language":"javascript","source_language":"en","source_url":"https://github.com/NotWoods/rollup-plugin-consts","tags":["javascript","rollup","rollup-plugin","const","consts","constant","typescript"],"install":[{"cmd":"npm install rollup-plugin-consts","lang":"bash","label":"npm"},{"cmd":"yarn add rollup-plugin-consts","lang":"bash","label":"yarn"},{"cmd":"pnpm add rollup-plugin-consts","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: requires Rollup >=1.15.0 <4","package":"rollup","optional":false}],"imports":[{"note":"Default export only; CommonJS require() cannot destructure default export.","wrong":"const { consts } = require('rollup-plugin-consts')","symbol":"consts","correct":"import consts from 'rollup-plugin-consts'"},{"note":"Constants are virtual modules prefixed with 'consts:'. Do not import from the plugin package itself.","wrong":"import environment from 'rollup-plugin-consts'","symbol":"environment (consts:environment)","correct":"import environment from 'consts:environment'"},{"note":"Virtual modules are default exports; named destructuring does not work.","wrong":"import { config } from 'consts:config'","symbol":"config (consts:config)","correct":"import config from 'consts:config'"}],"quickstart":{"code":"// rollup.config.js\nimport consts from 'rollup-plugin-consts';\n\nexport default {\n  input: 'src/index.js',\n  output: { file: 'dist/bundle.js', format: 'es' },\n  plugins: [\n    consts({\n      environment: 'production',\n      version: '1.0.0',\n      config: { names: ['foo', 'bar'] }\n    })\n  ]\n};\n\n// src/index.js\nimport environment from 'consts:environment';\nimport version from 'consts:version';\nimport config from 'consts:config';\n\nconsole.log(environment, version, config.names);","lang":"javascript","description":"Shows how to set up the plugin in rollup.config.js and use imported constants from virtual modules."},"warnings":[{"fix":"Use an alternative like @rollup/plugin-replace or migrate to a newer plugin.","message":"rollup-plugin-consts is incompatible with Rollup 4+. Peer dependency specifies rollup >=1.15.0 <4.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Consider using @rollup/plugin-replace or rollup-plugin-swc with define.","message":"Package has not been updated since 2020. May not receive fixes for future Rollup versions.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Pass only plain JSON-serializable values as constants.","message":"Constants are evaluated at build time and must be serializable (strings, booleans, objects). Functions or complex expressions are not supported.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Create a .d.ts file, e.g., declare module 'consts:*' { const value: any; export default value; }","message":"Virtual modules use the 'consts:' prefix and must be explicitly listed in TypeScript declarations.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Ensure consts() is called in the rollup plugins array and that it appears early (before plugins like commonjs).","cause":"The plugin is not registered in the rollup config plugins array or is placed after other plugins that cache resolution.","error":"Error: Cannot find module 'consts:environment'"},{"fix":"Use the default import: import consts from 'rollup-plugin-consts';","cause":"Using a named import instead of default import, or using CommonJS require without .default.","error":"[!] TypeError: consts is not a function"},{"fix":"Pass only strings, numbers, booleans, arrays, or plain objects as constant values.","cause":"The constant value contains a non-JSON literal (e.g., a function) that Rollup cannot parse as JavaScript.","error":"RollupError: Unexpected token (Note that you need plugins to import files that are not JavaScript)"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}