Snowpack
Snowpack is a lightweight, ESM-powered frontend build tool designed for rapid development cycles by serving applications unbundled. It introduced "O(1) Build Tooling," where only changed files are recompiled, leading to near-instant Hot Module Replacement (HMR) and dev server startup times (under 50ms). Unlike traditional bundlers that rebuild entire applications on every save, Snowpack leverages native ES Modules in development, effectively offloading complex bundling tasks to a dedicated production build step using tools like Webpack or Rollup. The project's last stable release was 3.8.8. While Snowpack pioneered a novel approach to frontend development and gained significant traction, it has since been largely superseded. The project is no longer actively maintained, with its core ideas and principles having evolved into other actively developed tools, most notably Vite, which was created by Snowpack's original architect. This makes it an unsuitable choice for new projects requiring ongoing support.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax within an ES Module context, which Snowpack typically operates in.fixReplace `require()` calls with `import` statements. Ensure your configuration and source files are treated as ES Modules (e.g., by using `.mjs` extension for config, or `"type": "module"` in `package.json`). -
Error: Cannot find module 'snowpack'
cause Snowpack is not installed or not found in the project's `node_modules`. This can happen if `npm install` was not run, or if it's installed globally but not linked correctly.fixRun `npm install snowpack` or `yarn add snowpack` in your project directory. If using global installation for CLI, ensure it's in your PATH or link it locally. -
Port 8080 is already in use
cause Another process is already using the default development server port (8080) that Snowpack tries to bind to.fixChange the `port` option in your `snowpack.config.mjs` (e.g., `devOptions: { port: 3000 }`) or specify it via CLI (`snowpack dev --port 3000`).
Warnings
- breaking Snowpack is no longer actively maintained and is not recommended for new projects. Its development has ceased, and its creators have moved on to other tools like Vite. Expect no further updates, security patches, or community support.
- breaking Snowpack v3 introduced several breaking changes including configuration file renames (e.g., 'homepage' no longer sets 'baseUrl'), changes to relative path resolution in config files, and clearer build file naming conventions.
- gotcha Snowpack is an ESM-first tool. While it can process CommonJS dependencies, custom build scripts and programmatic API usage should adhere to ES Module syntax. Using `require()` for Snowpack's own API will lead to errors in most modern Node.js environments.
- gotcha By default, Snowpack's `build` command generates an unbundled output optimized for modern browsers with native ESM. For traditional bundled and optimized production deployments (e.g., for older browser support or single-file delivery), it explicitly recommends integrating a separate bundler like Webpack or Rollup via plugins.
- gotcha The Node.js `engines` field specifies `>=10.19.0`. As Snowpack is unmaintained, it may not be compatible with newer Node.js versions, potentially leading to unexpected errors or installation failures with future Node.js releases.
Install
-
npm install snowpack -
yarn add snowpack -
pnpm add snowpack
Imports
- startServer
const { startServer } = require('snowpack');import { startServer } from 'snowpack'; - build
const { build } = require('snowpack');import { build } from 'snowpack'; - SnowpackConfig
import type { SnowpackConfig } from 'snowpack';
Quickstart
{
"name": "my-snowpack-app",
"version": "1.0.0",
"scripts": {
"start": "snowpack dev",
"build": "snowpack build"
},
"devDependencies": {
"snowpack": "^3.8.8",
"typescript": "^4.0.0"
}
}
// snowpack.config.mjs
export default {
mount: {
'public': '/',
'src': '/_dist_',
},
plugins: ['@snowpack/plugin-typescript'],
optimize: {
bundle: true,
minify: true,
target: 'es2018',
},
devOptions: {
open: 'none',
},
buildOptions: {
out: 'build',
clean: true,
},
};
// public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snowpack App</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/_dist_/index.js"></script>
</body>
</html>
// src/index.ts
const app = document.getElementById('root');
if (app) {
app.innerHTML = `<h1>Hello from Snowpack & TypeScript!</h1>`;
}
console.log('Snowpack development server is running...');