{"id":19267,"library":"babel-preset-const-enum","title":"babel-preset-const-enum","description":"Babel preset for transforming TypeScript const enums. Version 1.0.0. It runs babel-plugin-const-enum only on .ts/.tsx files to prevent syntax errors on non-TypeScript files. Provides two transform options: removeConst (default) reverts const enum to regular enum, while constObject inlines values into a const object literal for minification. Must be placed after @babel/preset-typescript in the preset order to work correctly. Lightweight, focused solution for using const enums with Babel.","status":"active","version":"1.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/dosentmatter/babel-preset-const-enum","tags":["javascript","babel-preset","typescript","const","enum","terser","uglify","minify","compress"],"install":[{"cmd":"npm install babel-preset-const-enum","lang":"bash","label":"npm"},{"cmd":"yarn add babel-preset-const-enum","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-preset-const-enum","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"peer dependency required for Babel plugin functionality","package":"@babel/core","optional":false}],"imports":[{"note":"Preset is CommonJS; use require in .babelrc or Babel config file.","wrong":"import preset from 'babel-preset-const-enum'","symbol":"default export (preset)","correct":"module.exports = require('babel-preset-const-enum')"},{"note":"Short name 'const-enum' is recommended in preset array, but full name also works.","wrong":"presets: [['babel-preset-const-enum', { transform: 'constObject' }]]","symbol":"Preset options","correct":"presets: [['const-enum', { transform: 'constObject' }]]"},{"note":"Use the preset instead; plugin may cause errors on non-TS files.","wrong":"import babelPluginConstEnum from 'babel-plugin-const-enum'","symbol":"Plugin (babel-plugin-const-enum) - not typically imported directly","correct":"not applicable"}],"quickstart":{"code":"// .babelrc\n{\n  \"presets\": [\n    \"@babel/preset-typescript\",\n    [\"const-enum\", { \"transform\": \"constObject\" }]\n  ]\n}\n\n// example.ts\nconst enum Color {\n  Red = 1,\n  Green = 2,\n  Blue = 3\n}\nconsole.log(Color.Red);\n\n// Run: npx babel example.ts --out-file example.js\n// Output:\n// const Color = {\n//   Red: 1,\n//   Green: 2,\n//   Blue: 3\n// };\n// console.log(Color.Red);\n","lang":"typescript","description":"Shows how to configure babel-preset-const-enum with @babel/preset-typescript and constObject transform, then run Babel on a .ts file."},"warnings":[{"fix":"Put 'const-enum' after '@babel/preset-typescript' in the preset order.","message":"Preset ordering: babel-preset-const-enum must come AFTER @babel/preset-typescript in the presets array.","severity":"gotcha","affected_versions":"all"},{"fix":"Use transform: 'constObject' if you want inlined values for minification.","message":"Transform 'removeConst' only removes const keyword, does not inline values. May not achieve full minification.","severity":"gotcha","affected_versions":"all"},{"fix":"Set transform: 'constObject' explicitly for production builds.","message":"The default transform 'removeConst' may not be optimal for production; consider using 'constObject' to fully leverage Uglify/Terser.","severity":"deprecated","affected_versions":"<=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Reorder presets: put '@babel/preset-typescript' first, then 'const-enum'.","cause":"babel-preset-const-enum ran before TypeScript preset, causing parsing failure on const enum syntax.","error":"SyntaxError: Unexpected token, expected \";\" (1:14) ... while parsing unexpected token"},{"fix":"Install: npm install --save-dev babel-preset-const-enum","cause":"Package not installed or not in node_modules.","error":"Error: Cannot find module 'babel-preset-const-enum'"},{"fix":"Use transform: 'constObject' to create a const object that avoids hoisting issues.","cause":"Using transform: 'removeConst' and the enum is used before declaration (hoisting issue).","error":"ReferenceError: MyEnum is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}