Babylon.js Project Scaffolding CLI
create-babylonjs is a command-line interface (CLI) tool designed to quickly scaffold new Babylon.js projects. It simplifies project setup by offering choices for module format (ES6 for tree-shaking or UMD for global `BABYLON` namespace), language (TypeScript or JavaScript), and bundler (Vite, Webpack, Rollup, or no bundler for CDN-only). The tool ensures a ready-to-run project that includes a starter scene, glTF model loading, environment lighting, and proper side-effect imports for optimized ES6 builds. Currently at version 9.3.3, it tracks the rapid development cycle of the Babylon.js core library, which typically sees new minor versions released weekly and major versions annually. This CLI is crucial for developers seeking a streamlined entry into Babylon.js development without the overhead of manual project configuration.
Common errors
-
Module not found: Error: Can't resolve '@babylonjs/core'
cause Incorrect or incomplete import path in an ES6 module project, often caused by trying to import from the root package instead of a deep path.fixEnsure imports specify the full path to the module, e.g., `import { Engine } from '@babylonjs/core/Engines/engine';`. -
BABYLON is not defined
cause Attempting to access the `BABYLON` global object in a project configured for ES6 modules without a UMD import, or in a CDN-based project where the `babylon.js` script tag is missing or loaded incorrectly.fixIf using UMD, ensure `import * as BABYLON from 'babylonjs';` is present. If using CDN, verify the `babylon.js` script tag is correctly placed in `index.html`. -
Uncaught TypeError: scene.createDefaultCamera is not a function
cause Missing the necessary side-effect import for Babylon.js helper functions or specific features like `createDefaultCamera` or `createDefaultEnvironment`.fixAdd `import '@babylonjs/core/Helpers/sceneHelpers';` to the top of your main scene file to register these helper functions.
Warnings
- breaking Babylon.js core library, used by generated projects, upgraded to TypeScript 6.0 in version 9.1.0. Older projects upgrading Babylon.js manually might encounter type conflicts if their TypeScript version is not updated accordingly.
- gotcha To leverage tree-shaking and optimize bundle size in ES6 projects, always use deep imports (e.g., `import { Engine } from '@babylonjs/core/Engines/engine';`) instead of root imports (e.g., `import { Engine } from '@babylonjs/core';`).
- gotcha Forgetting to include side-effect imports for essential features like glTF loaders (`import '@babylonjs/loaders/glTF';`) or certain materials (`import '@babylonjs/core/Materials/standardMaterial';`) will lead to runtime errors when attempting to use those features.
- gotcha The 'None' bundler option generates a CDN-only project without `npm` dependencies or a development server. It is suitable for simple experiments or learning but lacks modern development features like hot-reloading or optimized production builds.
- breaking Babylon.js 9.0 introduced significant updates to rendering, including major changes to how main materials generate WGSL shader code with WebGPU. Custom shaders or advanced material plugins might require adjustments.
Install
-
npm install create-babylonjs -
yarn add create-babylonjs -
pnpm add create-babylonjs
Imports
- Engine, Scene
import { Engine, Scene } from '@babylonjs/core';import { Engine, Scene } from '@babylonjs/core/Engines/engine'; import { Scene } from '@babylonjs/core/scene'; - GLTF Loader
SceneLoader.AppendAsync(file, scene); // Errors without loader import
import '@babylonjs/loaders/glTF';
- BABYLON (UMD)
const BABYLON = require('babylonjs'); // Incompatible with modern bundlers in ESM projectsimport * as BABYLON from 'babylonjs';
Quickstart
npm create babylonjs cd my-babylonjs-app npm install npm run dev