imports-loader
raw JSON →A webpack loader that automatically injects import/require statements for global variables into modules, enabling third-party code that depends on globals like $ or this to work with webpack's module system. Current stable version is 5.0.0 (January 2024), released under the webpack-contrib organization. It supports inline query syntax and webpack config rules, offering fine-grained control over import type (ESM, CommonJS), variable names, and module aliases. Key differentiators: explicit control over shimming via loader query parameters, support for named and default imports, and ability to wrap code in an IIFE with custom arguments. Requires webpack 5 and Node 18.12+. Releases follow semver with major bumps for Node and webpack version requirements.
Common errors
error Module parse failed: The keyword 'import' is reserved ↓
error SyntaxError: Unexpected token '|' ↓
imports-loader?imports=syntax|module|name,nextSyntax|module|name!./file.js error Error: Cannot find module 'imports-loader' ↓
npm install imports-loader --save-dev Warnings
breaking Minimum supported Node.js version is 18.12.0 ↓
breaking Minimum supported webpack version is 5 ↓
breaking Inline syntax changed: [] is no longer supported for multiple imports; use comma separator ↓
gotcha Existing import/require statements in the original code may conflict with injected imports ↓
gotcha By default, loader generates ES module named syntax (import). For CommonJS, set type: 'commonjs' ↓
Install
npm install imports-loader yarn add imports-loader pnpm add imports-loader Imports
- default (loader in webpack config or inline) wrong
Using require('imports-loader') directly in code instead of webpack configcorrectmodule.exports = { module: { rules: [ { test: /example\.js$/, use: [{ loader: 'imports-loader', options: { imports: ['default jquery $'] } }] } ] } } - imports-loader with type commonjs and wrapper wrong
Using older inline syntax with [] brackets (e.g., imports[]=default|jquery|$&imports[]=angular)correctconst myLib = require(`imports-loader?type=commonjs&imports=single|jquery|$&wrapper=window!./example.js`); - named import via loader wrong
Using default import syntax for named exports (e.g., imports=default|library|myMethod) would try to import default instead of named export.correctimport myLib from 'imports-loader?imports=named|library|myMethod,angular!./example.js'; - additionalCode option wrong
Using newline characters or unescaped spaces in inline query stringcorrectimport myLib from 'imports-loader?additionalCode=var%20myVariable%20=%20false;!./example.js';
Quickstart
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: require.resolve('example.js'),
use: [
{
loader: 'imports-loader',
options: {
imports: [
'default jquery $',
'default angular angular',
'named lodash map'
],
type: 'module' // default is ESM
}
}
]
}
]
}
};
// example.js
// Original code using global $:
$('button').click(function() { console.log('clicked'); });
// After loader processes, the file effectively starts with:
// import $ from 'jquery';
// import angular from 'angular';
// import { map } from 'lodash';