{"id":19116,"library":"babel-plugin-parameter-decorator","title":"Babel Plugin Parameter Decorator","description":"A Babel plugin that transforms function parameter decorators to behave like TypeScript parameter decorators. Current stable version is 1.0.16, with infrequent releases. It enables parameter decorators on constructor parameters and method parameters when using Babel with legacy decorator support. Unlike TypeScript's built-in support, this plugin requires the separate `@babel/plugin-proposal-decorators` with `legacy: true` and optionally `@babel/plugin-proposal-class-properties`. It is the only dedicated Babel plugin for parameter decorator transformation as of 2024, filling a gap left by other decorator plugins. Works with both JavaScript and TypeScript via Babel presets.","status":"active","version":"1.0.16","language":"javascript","source_language":"en","source_url":"https://github.com/WarnerHooh/babel-plugin-parameter-decorator","tags":["javascript","babel","babel-plugin","function","parameter","decorators","typescript"],"install":[{"cmd":"npm install babel-plugin-parameter-decorator","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-parameter-decorator","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-parameter-decorator","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer/ runtime dependency; must be configured with legacy: true for this plugin to work.","package":"@babel/plugin-proposal-decorators","optional":false}],"imports":[{"note":"This plugin is a Babel plugin, not a JavaScript module import. It is used only in Babel configuration. Must be placed after the decorators plugin.","wrong":"{\n  \"plugins\": [\"babel-plugin-parameter-decorator\"] // missing @babel/plugin-proposal-decorators\n}","symbol":"babel-plugin-parameter-decorator","correct":"// in .babelrc or babel.config.js\n{\n  \"plugins\": [\n    [\"@babel/plugin-proposal-decorators\", { \"legacy\": true }],\n    \"babel-plugin-parameter-decorator\"\n  ]\n}"}],"quickstart":{"code":"// Install dependencies\n// npm install --save-dev @babel/core @babel/cli @babel/plugin-proposal-decorators babel-plugin-parameter-decorator\n\n// .babelrc\n{\n  \"plugins\": [\n    [\"@babel/plugin-proposal-decorators\", { \"legacy\": true }],\n    \"babel-plugin-parameter-decorator\"\n  ]\n}\n\n// input.js\nfunction required(key) {\n  return function (target, propertyKey, parameterIndex) {\n    const metadata = `meta_${propertyKey}`;\n    target[metadata] = [\n      ...(target[metadata] || []),\n      { index: parameterIndex, key }\n    ];\n  };\n}\n\nfunction validate(target, property, descriptor) {\n  const fn = descriptor.value;\n  descriptor.value = function(...args) {\n    const metadata = `meta_${property}`;\n    target[metadata].forEach(function(meta) {\n      if (args[meta.index] === undefined) {\n        throw new Error(`${meta.key} is required`);\n      }\n    });\n    return fn.apply(this, args);\n  };\n  return descriptor;\n}\n\nclass Greeter {\n  constructor(message) {\n    this.greeting = message;\n  }\n\n  @validate\n  greet(@required('name') name) {\n    return \"Hello \" + name + \", \" + this.greeting;\n  }\n}\n\n// Run: npx babel input.js --out-file output.js","lang":"javascript","description":"Shows how to install, configure Babel with plugin-proposal-decorators (legacy mode) and this plugin, and then transpile a class with parameter decorators."},"warnings":[{"fix":"Ensure order: [['@babel/plugin-proposal-decorators', {legacy: true}], 'babel-plugin-parameter-decorator']","message":"Plugin must be placed after @babel/plugin-proposal-decorators in plugins array.","severity":"gotcha","affected_versions":"*"},{"fix":"Set 'onlyRemoveTypeImports' to true in preset options: ['@babel/preset-typescript', { onlyRemoveTypeImports: true }]","message":"If using @babel/preset-typescript, TypeScript-only imports used in decorators will be removed by default, breaking references.","severity":"gotcha","affected_versions":"*"},{"fix":"Use legacy: true (the Stage 1 decorators proposal).","message":"Requires @babel/plugin-proposal-decorators with legacy: true; the newer '2023-01' or '2023-11' decorator proposal is not supported.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add [\"@babel/plugin-proposal-decorators\", { \"legacy\": true }] before babel-plugin-parameter-decorator.","cause":"Missing @babel/plugin-proposal-decorators plugin in Babel config.","error":"Error: [BABEL] unknown: Decorators are not enabled."},{"fix":"Ensure a method decorator writes the metadata that the parameter decorator expects; example uses @validate.","cause":"Parameter decorator used on a method without the companion method decorator (e.g., @validate) writing metadata.","error":"TypeError: Cannot read properties of undefined (reading 'forEach')"},{"fix":"Define parameter decorator correctly: function decorator(key) { return function(target, propKey, paramIndex) { ... }; }","cause":"Incorrect import or invalid decorator function; parameter decorator must return a function that takes (target, propertyKey, parameterIndex).","error":"Error: Parameter decorator is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}