Skills Development Setting Up WebContainers and StackBlitz

Setting Up WebContainers and StackBlitz

v20260423
stackblitz-install-auth
This guide details how to install and configure the WebContainer API, enabling client-side Node.js execution directly in the browser. It also provides instructions for integrating the StackBlitz SDK, which is used for embedding highly interactive code editors into web applications. The guide covers necessary dependency installations, code examples, and crucial HTTP header configuration (COOP/COEP) required for cross-origin isolation and secure operation.
Get Skill
280 downloads
Overview

StackBlitz Install & Auth

Overview

Set up the WebContainer API for running Node.js in the browser, or the StackBlitz SDK for embedding interactive code editors. WebContainers require no auth -- they run entirely client-side. The StackBlitz SDK is for embedding projects from stackblitz.com.

Prerequisites

  • Node.js 18+ for build tooling
  • Modern browser with SharedArrayBuffer support (requires HTTPS + COOP/COEP headers)

Instructions

Step 1: Install WebContainer API

npm install @webcontainer/api

Step 2: Install StackBlitz SDK (for embedding)

npm install @stackblitz/sdk

Step 3: Configure Required HTTP Headers

WebContainers require cross-origin isolation. Add these headers to your server:

// Express middleware
app.use((req, res, next) => {
  res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
  res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
  next();
});
// Vite config
export default defineConfig({
  server: {
    headers: {
      'Cross-Origin-Embedder-Policy': 'require-corp',
      'Cross-Origin-Opener-Policy': 'same-origin',
    },
  },
});

Step 4: Verify WebContainer Boot

import { WebContainer } from '@webcontainer/api';

const wc = await WebContainer.boot();
console.log('WebContainer booted successfully');

// Verify filesystem works
await wc.mount({ 'test.txt': { file: { contents: 'Hello WebContainers!' } } });
const content = await wc.fs.readFile('/test.txt', 'utf-8');
console.log(`File content: ${content}`);

Output

WebContainer booted successfully
File content: Hello WebContainers!

Error Handling

Error Cause Solution
SharedArrayBuffer is not defined Missing COOP/COEP headers Add cross-origin isolation headers
Failed to boot Multiple instances Only one WebContainer per page
Not in secure context HTTP instead of HTTPS Use HTTPS or localhost

Resources

Next Steps

Proceed to stackblitz-hello-world for your first WebContainer project.

Info
Category Development
Name stackblitz-install-auth
Version v20260423
Size 2.78KB
Updated At 2026-04-28
Language