Registry / devops / exports-loader

exports-loader

JSON →
library5.0.0jsnpmunverified

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.

npm install exports-loader
INSTALL
IMPORT
SIG · EXPORTS-LOADER
E
exports-loader
devopsjavascriptv5.0.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

inline import syntax
✓ import { foo } from 'exports-loader?exports=foo!./file.js'
✗ import foo from 'exports-loader?exports=named|foo!./file.js'
inline loader syntax with query string: 'exports=foo' is shorthand for 'named|foo'. Use '|' or '%20' as separator.
require() syntax
✓ const { foo } = require('exports-loader?type=commonjs&exports=foo!./file.js')
✗ const { foo } = require('exports-loader?exports=foo!./file.js')
When using CommonJS require(), you must set ?type=commonjs to generate module.exports. Omitting creates ES exports and might break CJS code.
webpack config rule
✓ module: { rules: [ { test: require.resolve('./vendor.js'), loader: 'exports-loader', options: { exports: 'myFunc' } } ] }
✗ module: { rules: [ { test: /vendor\.js$/, loader: 'exports-loader?exports=myFunc' } ] }
Use test with require.resolve() to match specific file path, not regex to avoid applying to multiple modules.

Shows webpack config rule using exports-loader to add a default export to a legacy script, then importing it in modern code.

// webpack.config.js module.exports = { module: { rules: [{ // match a specific file that lacks exports test: require.resolve('./path/to/legacy.js'), loader: 'exports-loader', options: { // export a function named 'myFunc' as default exports: 'default myFunc', }, }], }, }; // then in your code import myFunc from './path/to/legacy.js'; myFunc();
Debug
Known issues
breakingMinimum Node.js version changed to 18.12.0 (v5.0.0) and 14.15.0 (v4.0.0).
fix
Update to Node.js >=18.12.0 or use v4.0.0 if locked to Node 14/16.
affects: >=5.0.0
breakingMinimum webpack version changed to 5 (v2.0.0). Inline syntax changed: '[]' no longer supported, use '|' or '%20'.
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).
affects: >=2.0.0
deprecatedThe 'exports' option string syntax using brackets (e.g., 'exports[]=myFunc') is removed in v2.0.0.
fix
Use pipe-separated syntax: 'exports=myFunc' for named (shorthand) or 'exports=named|myFunc'.
affects: >=2.0.0
gotchaLoader generates ES module named exports by default (export { ... }). If consuming with CommonJS require(), you must set ?type=commonjs or the exports will be incorrect.
fix
Add ?type=commonjs to inline query or set options.type: 'commonjs' in webpack config rule.
affects: >=1.0.0
gotchaExisting exports in the original file can conflict with injected exports, causing build errors.
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.
affects: >=0.0.0
gotchaUsing test regex (e.g., /vendor\.js$/) instead of require.resolve() may accidentally match multiple files and apply exports-loader unexpectedly.
fix
Use require.resolve() in test to match exact file path, or use a more specific regex (e.g., include file path anchor).
affects: >=0.0.0
Errors
Common errors & fixes
Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type.
The source file is not a JavaScript module or exports-loader cannot add exports because file is malformed.
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.
Error: exports is not defined
Using CommonJS require() without type=commonjs; generated ES export causes ReferenceError in Node.js CommonJS context.
fix
Add ?type=commonjs to inline query or set type: 'commonjs' in webpack config options.
Cannot find module 'exports-loader'
exports-loader not installed or not in node_modules.
fix
Run npm install exports-loader --save-dev or yarn add -D exports-loader.
Error: 'exports' value is not a string or string[]
Options.exports is not defined correctly (e.g., using array in v2+ when only string expected).
fix
In v2+, options.exports should be a string or array of strings. For inline syntax, use query parameter 'exports=...'. Check documentation.
Upgrade
Version history
5.0.0latest on npm
Audit
Dependencies
webpackrequiredrequired as peer dependency
Agent activity
4 hits · last 30 days
node
4
Resources
exports-loader — npm install exports-loader · libregistry