Skip to content

BabelJs - Environment Setup

To effectively use BabelJS in your projects, you’ll need a basic development environment. This chapter outlines the essential tools and steps to get started.

The primary components for a BabelJS setup are:

  • Node.js and npm (Node Package Manager)
  • A Text Editor or Integrated Development Environment (IDE)
  • Babel packages (installed per project)

Node.js is a JavaScript runtime environment that allows you to run JavaScript code outside of a web browser. npm is the default package manager for Node.js and is used to install and manage project dependencies, including Babel itself.

Open your terminal or command prompt and type the following commands to check if Node.js and npm are already installed:

node -v
npm -v

If these commands output version numbers (e.g., v18.12.1 for Node.js, 8.19.2 for npm), they are installed. It’s generally recommended to use a Long-Term Support (LTS) version of Node.js for stability.

If Node.js and npm are not installed, or if you need to update them:

  • Go to the official Node.js website: https://nodejs.org/
  • Download the installer for your operating system (Windows, macOS, Linux). It’s usually best to choose the LTS version.
  • Run the installer and follow the on-screen instructions.

npm is included with the Node.js installation, so you don’t need to install it separately. After installation, re-run node -v and npm -v in a new terminal window to confirm.

Alternatively, you can use a version manager like nvm (Node Version Manager) to install and manage multiple Node.js versions on your system. This is highly recommended for developers working on multiple projects that might require different Node.js versions. More information on nvm can be found here: https://github.com/nvm-sh/nvm

You’ll need a code editor to write your JavaScript and configuration files. Popular choices among developers include:

  • Visual Studio Code (VS Code) - Free, highly extensible, with excellent JavaScript/TypeScript support and integrated terminal.
  • WebStorm - Powerful commercial IDE with advanced features for web development.
  • Sublime Text - Lightweight and fast, extensible via plugins.
  • Atom - Free, open-source, and customizable.

Choose an editor that you are comfortable with and that offers good syntax highlighting for JavaScript.

3. Babel Packages (Per-Project Installation)

Section titled “3. Babel Packages (Per-Project Installation)”

Babel itself is typically installed as a dependency within each project rather than globally on your system. This allows different projects to use different versions of Babel and its plugins/presets if needed.

The core Babel packages you’ll usually install in a project are:

  • @babel/core: The main Babel engine.
  • @babel/cli: The command-line tool for running Babel (if you plan to use it directly).
  • @babel/preset-env: A smart preset that allows you to use the latest JavaScript features based on your target environments.

You’ll install these using npm within your project directory. For example:

# Navigate to your project directory
cd /path/to/your-project
# Initialize npm if it's a new project
npm init -y
# Install Babel packages as development dependencies
npm install --save-dev @babel/core @babel/cli @babel/preset-env

Further details on project setup and Babel configuration are covered in subsequent chapters.

Once you have Node.js/npm and a code editor, and you’ve installed Babel packages into a project, your basic environment is ready. The next steps involve:

  • Creating JavaScript files with modern syntax.
  • Configuring Babel (e.g., via a babel.config.json file) to specify presets and plugins.
  • Running Babel (either via the CLI or through a build tool like Webpack or Gulp) to transpile your code.

This foundational setup will allow you to follow the examples and tutorials in the rest of this guide. The specific Babel commands and configurations will be detailed in their respective chapters.

A typical project structure might look like this initially:

your-project/
├── node_modules/ (created by npm install)
├── src/ (your source JavaScript files)
│ └── index.js
├── package.json (npm project manifest)
├── package-lock.json (npm dependency lock file)
└── babel.config.json (Babel configuration)