Registry / devops / file-loader

file-loader

JSON →
library6.2.0jsnpmunverified

Resolves import/require on a file into a URL and emits the file into the output directory. Stable version 6.2.0 (last release Oct 2020, maintenance mode) with webpack 4/5 support. Key differentiator from alternatives (e.g., url-loader, webpack asset modules) is explicit file emission with custom naming, output path, and public path configuration. Supports hashing, ES modules, and sourceFilename.

npm install file-loader
INSTALL
IMPORT
SIG · FILE-LOADER
F
file-loader
devopsjavascriptv6.2.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

default
import img from './file.png';
import { img } from './file.png';
file-loader returns the default export as the URL string.
require
const img = require('./file.png');
const { img } = require('./file.png');
CommonJS usage returns the URL via module.exports.
webpack config
module.exports = { module: { rules: [ { test: /\.png$/, loader: 'file-loader' } ] } };
module.exports = { module: { rules: [ { test: /\.png$/, use: 'file-loader' } ] } };
use property can be a string or array; Objects with loader/options are also valid.

Shows basic webpack config and usage of file-loader with custom naming and output path.

// webpack.config.js module.exports = { module: { rules: [ { test: /\.(png|jpe?g|gif)$/i, loader: 'file-loader', options: { name: '[name].[contenthash].[ext]', outputPath: 'images' } } ] } }; // In your source file import image from './my-image.png'; console.log(image); // Output: /images/my-image.abc123.png
Debug
Known issues
breakingMinimum node version increased to 10.13.0 in v5.0.0
fix
Upgrade to Node >=10.13.0 or stay on v4.x
affects: <5.0.0
breakingesModule option defaults to true in v5.0.0, outputting ES modules instead of CommonJS
fix
Set esModule: false in options if CommonJS required
affects: <5.0.0
deprecatedesModules option renamed to esModule in v5.0.0
fix
Use esModule instead of esModules
affects: >=5.0.0
breakingDefault hash algorithm changed to md4 in v6.0.0. Older webpack configs relying on md5 may break consistency.
fix
Explicitly set hashFunction option to 'md5' if md5 needed
affects: >=6.0.0
gotchaWhen using outputPath with a function, the resourcePath is absolute. Relative path must be computed manually.
fix
Use path.relative(context, resourcePath) to get relative path
affects: >=4.0.0
Errors
Common errors & fixes
Module parse failed: Unexpected token (1:0). You may need an appropriate loader to handle this file type.
Missing or misconfigured file-loader rule for the file type.
fix
Add a rule for the file extension, e.g., { test: /\.png$/, loader: 'file-loader' }
Error: Cannot find module 'file-loader'
file-loader not installed.
fix
Run npm install file-loader --save-dev
TypeError: Cannot read property 'context' of undefined
Using outputPath as function without webpack 5 fallback.
fix
Use outputPath string or ensure webpack version compatibility
Unhandled rejection in promise: Error: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
Misconfigured loader options, e.g., misspelled option name.
fix
Check options names (name, outputPath, publicPath, etc.) against documentation
Upgrade
Version history
6.2.0latest on npm
Audit
Dependencies
webpackrequiredpeer dependency, required to operate as a loader
Agent activity
2 hits · last 30 days
node
2
Resources
file-loader — npm install file-loader · libregistry