Parcel Bundler
Parcel is a blazing fast, zero-configuration web application bundler designed for simplicity and performance. The current stable version is 2.16.4, with the project maintaining a frequent release cadence for minor and patch updates. Key differentiators include its 'zero-config' approach, aiming to get developers up and running quickly without extensive setup. Under the hood, Parcel leverages Rust-based tooling (such as its JavaScript compiler and HTML/SVG transformers since v2.15.0) for significant performance improvements in bundling, minification, and tree-shaking. It supports a wide range of web assets out-of-the-box, including JavaScript (ES modules, CommonJS), TypeScript, React Server Components (since v2.14.0), CSS, HTML, SVG, images, and more, with automatic code splitting and differential bundling. Parcel also offers a robust plugin system for extensibility and a programmatic API for custom build integrations.
Common errors
-
Error: Cannot find module 'parcel'
cause The `parcel` package or one of its core dependencies is not correctly installed or resolvable in the current project or global environment.fixEnsure `parcel` is installed locally: `npm install parcel --save-dev` or `yarn add parcel --dev`. If using global Parcel, ensure its path is in your system's PATH variable. -
TypeError: Cannot read properties of null (reading 'data') at BufferList.first (node:internal/streams/buffer_list:XX:YY)
cause This error typically indicates an issue with Node.js streams, often occurring during file processing or when Parcel attempts to read from a malformed or unavailable stream, possibly due to corrupted cache or a specific file type handling.fixTry clearing Parcel's cache (`rm -rf .parcel-cache`) and your project's `dist` or output directory. If the issue persists, ensure your Node.js version is compatible and all dependencies are correctly installed. This can sometimes be related to specific transformer or packager plugins. -
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'name' imported from ...
cause Parcel failed to resolve a module, often when dealing with local `file:` dependencies in `package.json`, monorepos, or incorrectly configured aliases/paths, particularly after migrating from Parcel 1 to 2, or with complex `tsconfig.json` setups.fixVerify that your `package.json` dependencies are correctly defined. For local dependencies, ensure paths are correct. For TypeScript `baseUrl` or `paths`, configure Parcel's resolver through `.parcelrc` or `package.json` aliases. Clear cache (`rm -rf .parcel-cache`) to ensure fresh resolution. -
`@parcel/fs tried to access @parcel/core (a peer dependency) but it isn't provided by its ancestors`
cause This error occurs when Parcel's internal packages, which have peer dependencies on each other (like `@parcel/fs` needing `@parcel/core`), are not installed in a flat `node_modules` structure, or when a package manager hoists dependencies incorrectly.fixEnsure you are using a recent version of npm (>=7) or Yarn (>=1) that handles peer dependencies correctly. Try clearing your `node_modules` and `package-lock.json`/`yarn.lock` and reinstalling (`npm install` or `yarn install`). Verify that `@parcel/core` and `@parcel/fs` are correctly installed. -
404 Page not found (when running `parcel serve` in development)
cause The development server started by `parcel serve` cannot find the specified entry HTML file, or the root directory configuration is incorrect, leading to a missing index page.fixDouble-check the path to your entry HTML file in the `parcel serve` command (e.g., `parcel serve src/index.html`). Ensure your `package.json`'s `main` field isn't inadvertently pointing to a non-existent file, or if you're using `package.json#main` for library export, explicitly specify the HTML entry in the CLI. Clear Parcel's cache (`rm -rf .parcel-cache`).
Warnings
- breaking Parcel v2 requires Node.js version 16.0.0 or higher. Installing Parcel on older Node.js versions (e.g., 14.x) will result in installation failures due to incompatible dependencies like `node-addon-api`. Ensure your Node.js environment meets the minimum requirement specified in `engines.node`.
- breaking Starting from Parcel v2.15.2, the minimum required `glibc` version on Linux systems is 2.26. Users on older Linux distributions (e.g., CentOS 7, Ubuntu 16.04) may encounter issues due to this C library dependency.
- gotcha Parcel 2 uses `package.json#main` as the output path for projects by default, especially if not explicitly configured otherwise. If you have a `main: "index.js"` entry from a default `npm init` in a web app, it can lead to unexpected output file locations or conflicts.
- breaking The `--out-dir` CLI flag from Parcel 1 has been renamed to `--dist-dir` in Parcel 2 to align with `package.json#targets` options.
- deprecated The `SWC` minifier became the default JavaScript minifier in Parcel v2.9.0, replacing `Terser`. While most `Terser` configuration options are supported, using a dedicated `.terserrc` file might not leverage the full performance benefits of `SWC`.
- gotcha Parcel v2.9.0 introduced support for `package.json` `"exports"` field, but it is opt-in. Enabling it can be a breaking change, as consumers can no longer import files not explicitly exported, potentially leading to 'dual package hazard' issues with different import/require conditions.
- gotcha The Parcel dev server's default CORS behavior was modified, and an explicit `--no-cors` option was added in v2.16.4. Depending on prior default behavior, this could affect local development environments expecting specific CORS headers.
Install
-
npm install parcel -
yarn add parcel -
pnpm add parcel
Imports
- Parcel
const Parcel = require('parcel');import { Parcel } from '@parcel/core'; - createWorkerFarm
import { createWorkerFarm } from '@parcel/core'; - Default config
import defaultConfig from '@parcel/config-default';
Quickstart
import { Parcel } from '@parcel/core';
import { createWorkerFarm } from '@parcel/core';
import defaultConfig from '@parcel/config-default';
import { MemoryFS } from '@parcel/fs';
// Create a worker farm for multi-threading, essential for Parcel's performance.
const workerFarm = createWorkerFarm();
async function buildProject() {
// Create an in-memory file system for output, useful for testing or server-side rendering.
const outputFS = new MemoryFS(workerFarm);
const bundler = new Parcel({
entries: ['./src/index.html'], // Your main entry point
defaultConfig,
workerFarm,
outputFS,
mode: 'production', // Build for production, enabling minification and tree-shaking
defaultTargetOptions: {
shouldOptimize: true,
sourceMaps: false,
engines: { node: '>= 16.0.0' }, // Ensure Node.js version is specified
},
});
try {
const { bundleGraph, buildTime } = await bundler.run();
console.log(`✨ Built in ${buildTime}ms`);
// You can access bundles from bundleGraph and read them from outputFS
const htmlBundle = Array.from(bundleGraph.get === 'html')[0];
if (htmlBundle) {
const content = await outputFS.readFile(htmlBundle.filePath, 'utf8');
console.log('Output HTML content sample:\n', content.substring(0, 500));
}
} catch (error) {
console.error('❌ Build failed:', error);
process.exit(1);
} finally {
await workerFarm.end(); // Clean up worker processes
}
}
buildProject();