webpack
Configurable module bundler that builds a dependency graph from entry points and emits optimized static assets for web and Node targets.
Official Documentation ↗v5. Check the official docs for the latest API surface.Installation
npm install --save-dev webpack webpack-clipnpm add -D webpack webpack-cliMethods
No methods match this filter.
Overview
webpack is the module bundler behind a large share of production web builds. It walks the import graph from entry points, applies loaders and plugins, and emits bundles, chunks, and assets. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
webpack— Creates a Compiler instance from a configuration or runs the build immediately when a callback is provided.compiler.run— Runs a single compilation pass and invokes the callback with the resulting stats.compiler.watch— Starts a persistent watch that rebuilds on file changes and returns a handle you can close.
Example
const webpack = require('webpack');
const config = require('./webpack.config.js');
webpack(config, (err, stats) => {
if (err) { console.error(err); return; }
console.log(stats.toString({ colors: true }));
});
Notes
Webpack 5 removed automatic Node.js polyfills, so browser code that needs Buffer or process must declare fallbacks explicitly. Use content hashing in filenames and keep the runtime chunk small to preserve long-term caching.
Verification
Build twice without editing sources and confirm hashed asset names are stable, then assert stats.hasErrors() is false for a clean run.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://webpack.js.org/api/node/ · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:53:28.064Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- The Node.js API is useful in scenarios in which you need to customize the build or development process since all the reporting and error handling must be done manually and webpack only does the compiling part. (https://webpack.js.org/api/node/)
- Those must be handled separately using stats.hasErrors() , which will be covered in detail in the Error Handling section of this guide. (https://webpack.js.org/api/node/)
- The final logging of stats and errors should be done in this callback function. (https://webpack.js.org/api/node/)
FAQ
- How do I programmatically read the emitted asset names?
- Inspect stats.toJson({ assets: true }).assets[].name, or use the compiler.hooks.afterEmit hook together with compilation.getPath(outputOptions).
- Does webpack support an ESM configuration file?
- Yes since v5. Name the file webpack.config.mjs, or set "type": "module" in package.json and use export default.
- How do I run webpack in watch mode from Node instead of the CLI?
- Call compiler.watch(options, handler); it returns a Watching instance whose close() method stops the watcher.
Report a problem with this API docOpens a pre-filled issue on GitHub.