{"id":20018,"library":"exports-loader","title":"exports-loader","description":"Webpack loader that adds export statements to source files that lack them. Current stable version is 5.0.0 (Jan 2024), requiring Node.js >=18.12.0 and webpack^5.0.0. Commonly used for shimming legacy scripts, vendor files, or modules that do not expose exports. Supports inline query configuration and webpack rule options to generate named or default ES module exports (default) or CommonJS module.exports. Key differentiator: declarative, composable with other loaders; simpler than writing wrapper modules. No security incidents reported.","status":"active","version":"5.0.0","language":"javascript","source_language":"en","source_url":"https://github.com/webpack-contrib/exports-loader","tags":["javascript","webpack"],"install":[{"cmd":"npm install exports-loader","lang":"bash","label":"npm"},{"cmd":"yarn add exports-loader","lang":"bash","label":"yarn"},{"cmd":"pnpm add exports-loader","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"required as peer dependency","package":"webpack","optional":false}],"imports":[{"note":"inline loader syntax with query string: 'exports=foo' is shorthand for 'named|foo'. Use '|' or '%20' as separator.","wrong":"import foo from 'exports-loader?exports=named|foo!./file.js'","symbol":"inline import syntax","correct":"import { foo } from 'exports-loader?exports=foo!./file.js'"},{"note":"When using CommonJS require(), you must set ?type=commonjs to generate module.exports. Omitting creates ES exports and might break CJS code.","wrong":"const { foo } = require('exports-loader?exports=foo!./file.js')","symbol":"require() syntax","correct":"const { foo } = require('exports-loader?type=commonjs&exports=foo!./file.js')"},{"note":"Use test with require.resolve() to match specific file path, not regex to avoid applying to multiple modules.","wrong":"module: { rules: [ { test: /vendor\\.js$/, loader: 'exports-loader?exports=myFunc' } ] }","symbol":"webpack config rule","correct":"module: { rules: [ { test: require.resolve('./vendor.js'), loader: 'exports-loader', options: { exports: 'myFunc' } } ] }"}],"quickstart":{"code":"// webpack.config.js\nmodule.exports = {\n  module: {\n    rules: [{\n      // match a specific file that lacks exports\n      test: require.resolve('./path/to/legacy.js'),\n      loader: 'exports-loader',\n      options: {\n        // export a function named 'myFunc' as default\n        exports: 'default myFunc',\n      },\n    }],\n  },\n};\n\n// then in your code\nimport myFunc from './path/to/legacy.js';\nmyFunc();","lang":"javascript","description":"Shows webpack config rule using exports-loader to add a default export to a legacy script, then importing it in modern code."},"warnings":[{"fix":"Update to Node.js >=18.12.0 or use v4.0.0 if locked to Node 14/16.","message":"Minimum Node.js version changed to 18.12.0 (v5.0.0) and 14.15.0 (v4.0.0).","severity":"breaking","affected_versions":">=5.0.0"},{"fix":"Upgrade webpack to v5 or stick to v1.x if on webpack 4. For inline syntax, replace [] with | (e.g., exports[]=myFunc -> exports=named|myFunc).","message":"Minimum webpack version changed to 5 (v2.0.0). Inline syntax changed: '[]' no longer supported, use '|' or '%20'.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Use pipe-separated syntax: 'exports=myFunc' for named (shorthand) or 'exports=named|myFunc'.","message":"The 'exports' option string syntax using brackets (e.g., 'exports[]=myFunc') is removed in v2.0.0.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Add ?type=commonjs to inline query or set options.type: 'commonjs' in webpack config rule.","message":"Loader generates ES module named exports by default (export { ... }). If consuming with CommonJS require(), you must set ?type=commonjs or the exports will be incorrect.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure source file has no exports or use exports-loader only on files that truly lack exports. Alternatively, use a shim or wrapper module.","message":"Existing exports in the original file can conflict with injected exports, causing build errors.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use require.resolve() in test to match exact file path, or use a more specific regex (e.g., include file path anchor).","message":"Using test regex (e.g., /vendor\\.js$/) instead of require.resolve() may accidentally match multiple files and apply exports-loader unexpectedly.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Ensure the file is valid JavaScript. If using inline syntax, check query string syntax. Try adding additional loaders (e.g., babel-loader) before exports-loader.","cause":"The source file is not a JavaScript module or exports-loader cannot add exports because file is malformed.","error":"Module parse failed: Unexpected token (1:0)\nYou may need an appropriate loader to handle this file type."},{"fix":"Add ?type=commonjs to inline query or set type: 'commonjs' in webpack config options.","cause":"Using CommonJS require() without type=commonjs; generated ES export causes ReferenceError in Node.js CommonJS context.","error":"Error: exports is not defined"},{"fix":"Run npm install exports-loader --save-dev or yarn add -D exports-loader.","cause":"exports-loader not installed or not in node_modules.","error":"Cannot find module 'exports-loader'"},{"fix":"In v2+, options.exports should be a string or array of strings. For inline syntax, use query parameter 'exports=...'. Check documentation.","cause":"Options.exports is not defined correctly (e.g., using array in v2+ when only string expected).","error":"Error: 'exports' value is not a string or string[]"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}