JavaScript Tools
Why should you care about JavaScript Tools?
JavaScript tools save time, reduce repetitive work, and help your code run reliably across environments. Once you understand the purpose of each tool, modern workflows feel simpler instead of overwhelming.
When you are just starting out, you write JavaScript in a single file and open it in the browser. That works great for small things. But the moment you start building real projects, you will run into a bunch of questions.
How do you use someone else's code without copying and pasting it? How do you combine ten different files into one for the browser? What do you do when a browser does not support the latest JavaScript features? How do you make sure your code runs the same way on Chrome, Firefox, and Safari?
JavaScript tools help you install packages, manage project files, prepare code for the browser, and make development easier.
We will start with Node.js and npm, then look at package files, code-quality tools, bundlers, Parcel, Babel, transpiling, and polyfills.
Good to know: Not every JavaScript project needs all these tools. A small project may only need a browser and a code editor, while larger applications often use package managers, code-quality tools and build tools as part of their development workflow.npm is a tool for installing and managing JavaScript packages.
The npm CLI is the command-line program you run in the terminal. The npm registry is the online service where developers publish packages.
A package is reusable code that someone has prepared for other projects. You can install a package instead of writing that code yourself.
npm is the default package manager distributed with Node.js. When you install Node.js using its standard installer, npm is installed alongside it. You can verify both installations using the following commands.
node --version
npm --version
To start a project, create a folder and run npm init -y to create a package.json with default values.
mkdir my-project
cd my-project
npm init -y # -y means accept all defaults, creates package.json quickly
The package.json file is the main configuration file for a JavaScript project. It contains information such as the project name, version, scripts, dependencies and development dependencies. Package managers use this file to understand which packages the project requires and which commands can be executed.
To install a package, you use npm install. For example, to install a popular utility library called lodash:
npm install lodash
After this command, npm downloads the package into node_modules and adds it to package.json.
- A node_modules folder is created with the actual package code inside it
- Your package.json is updated to list lodash as a dependency
Your package.json will look something like this:
{
"name": "my-project",
"version": "1.0.0",
"dependencies": {
"lodash": "^4.17.21"
}
}
dependencies are packages the app needs while it runs. devDependencies are tools used while building the project, such as ESLint, Prettier, test runners, and bundlers.
node_modules contains the installed package code. It is usually excluded from Git; teammates install the dependencies after downloading the project.
npm creates a package-lock.json file that records the resolved dependency tree, including the exact versions of installed packages and their dependency relationships. This helps npm reproduce consistent installations across different machines and environments.
The lock file should normally be committed to version control. When another developer clones the project, they can run npm ci to install the dependencies using the lock file without changing it.
A tiny npm cheat sheet:
| Command | Purpose |
|---|---|
npm install |
Installs dependencies and may update the lock file when package definitions change |
npm ci |
Performs a clean installation using the existing lock file |
npm install lodash |
Adds lodash as a dependency |
npm install --save-dev eslint |
Adds ESLint as a development dependency |
npm run build |
Runs the build script from package.json |
npm ci requires an existing lock file that agrees with package.json. It removes any existing node_modules directory before installing and does not update the lock file.
It is also smart to keep dependencies updated, use tools like npm audit, and be careful about what packages you install. Third-party packages are helpful, but they are also part of your software supply chain.
npm audit checks the dependency tree against known security advisories. It can identify vulnerable packages, but it does not guarantee that your application is completely secure. Review audit findings before applying fixes, because some updates can introduce breaking changes.
Interactive example for this topic.
The editor is where you write files; the terminal is where you run project commands. Keeping those jobs separate makes tool errors easier to understand.
- Install VS Code and open it.
- Create a folder called
my-project, then choose File > Open Folder in VS Code. - Choose Terminal > New Terminal. The prompt should show that terminal's current folder.
- Run
node --versionandnpm --version. If either command is not recognized, install the Node.js LTS release and restart VS Code. - Run
npm init -y. Confirm thatpackage.jsonappears in the Explorer panel.
| Command | What it does | When to use it |
|---|---|---|
pwd |
Displays the current directory in macOS/Linux shells and PowerShell. | To confirm you are in the project folder. In Windows Command Prompt, run cd with no arguments. |
cd folder-name |
Changes the current directory. | To enter the project folder. |
cd .. |
Moves to the parent directory. | To go up one folder. |
npm install |
Installs dependencies listed in package.json. |
After cloning a project or changing dependencies. |
npm install --save-dev package |
Adds a development-only tool. | For ESLint, Prettier, test runners, and bundlers. |
npm run script-name |
Runs a script from package.json. |
For commands such as lint, format, or build. |
npx lets you execute a package's command-line tool without having to install it globally. It is commonly used to run project tools such as ESLint and other development utilities. If the package is not installed locally, npx can fetch it, so check the package name before running it.
Example terminal session:
mkdir my-project
cd my-project
npm init -y
npm install --save-dev eslint prettier
npm install lodash
package.json, a lock file, and node_modules. Do not edit node_modules by hand; use npm commands instead.
ESLint finds likely JavaScript problems and enforces agreed code rules. Prettier formats code consistently. ESLint asks “could this be wrong?”; Prettier asks “can this be formatted consistently?” They complement each other.
Prettier focuses on code formatting, such as indentation, quotes and line breaks. ESLint focuses on code quality and potential errors, such as unused variables, problematic patterns and rule violations. Some ESLint rules can overlap with formatting, so teams often configure the tools to work together without conflicting rules.
npm install --save-dev eslint prettier
npx eslint --init
npx prettier --write .
ESLint configuration depends on the ESLint version and project setup. Modern ESLint versions use the flat configuration format, commonly stored in eslint.config.js. Follow the setup prompts for the version installed in your project.
Add scripts so teammates do not need to remember long commands:
{
"scripts": {
"lint": "eslint .",
"format": "prettier --write .",
"format:check": "prettier --check ."
}
}
npm run lint
npm run format
npm run format:check
eslint . checks files selected by the project's ESLint configuration and ignore rules; it does not necessarily lint every file in the repository.
In VS Code, install the official ESLint and Prettier extensions, then enable format-on-save if you want formatting to happen automatically. Keep the project configuration in the repository so the terminal, editor, and CI use the same rules.
Do not confuse them: Prettier may reformat a file without proving its logic is correct. ESLint may flag a problem without changing the code. Run both.A package manager is a tool that installs, updates, and removes packages for you. npm, Yarn, and pnpm are the three package managers you will see most often.
- Yarn - a popular alternative to npm with similar commands.
- pnpm - uses a content-addressable store and links packages into projects. This can reduce duplicated package storage when multiple projects use the same dependencies.
All three read package.json, install the required packages, and use a lock file. Their commands and folder details differ slightly. Follow the choice made by your project or team.
Here is a quick comparison of common commands across the three:
# Install all dependencies
npm install
yarn install
pnpm install
# Add a new package
npm install react
yarn add react
pnpm add react
# Remove a package
npm uninstall react
yarn remove react
pnpm remove react
# Run a script defined in package.json
npm run build
yarn build
pnpm run build
One thing all package managers share is the concept of a lock file. This file records the resolved dependency versions that were installed. NPM creates package-lock.json, Yarn creates yarn.lock, and pnpm creates pnpm-lock.yaml.
The lock file matters because it makes installs much more reproducible across teammates, CI, and deployments. It greatly reduces surprises, even though it does not guarantee the exact same environment in every situation.
Although npm, Yarn and pnpm solve the same general problem, they do not use interchangeable lock files. A project should normally use one package manager consistently. Avoid switching package managers casually because it can create multiple lock files and inconsistent dependency installations.
Tip: Always commit your lock file to version control. It helps keep installs consistent across your team and deployment environments.As your project grows, you start splitting your code into multiple files and modules. That is good practice. Modern browsers can load ES modules directly, so bundlers are not mandatory for every project.
That is where a bundler can help. A bundler takes the files in your project, follows their imports, and prepares the output for the browser.
A bundler is responsible for processing a project's dependency graph and preparing files for deployment. Modern tools such as Vite provide more than bundling: they also offer a development server, fast module updates and a production build pipeline. This is why Vite is often described as a build tool rather than only a bundler.
A bundler does a few key things:
- Follows imports - finds the code used by each import statement
- Combines or splits files - creates one file or smaller chunks when that helps loading
- Tree shaking – identifies and removes unused code from the production output when the module structure and side-effect information allow it.
- Minification - removes unnecessary spaces and shortens some names to reduce file size
Compare these tools by what they do:
| Tool | Main purpose |
|---|---|
| Webpack | Highly configurable module bundler used in many established projects |
| Parcel | Bundler designed to work with minimal configuration |
| Vite | Modern frontend development and build tool with a fast development server |
| Rollup | Bundler commonly used to build JavaScript libraries |
Without a bundler, you can still use the browser's native ES module system. Bundlers become especially helpful when you want optimised production builds, code splitting, asset handling, and other build-time improvements.
Good to know: Modern frameworks and starters often come with these build tools pre-configured. You might be using one without even knowing it.Parcel is a bundler that needs very little configuration. It is useful for beginners because you can start with an HTML file instead of learning many settings first.
Parcel follows the JavaScript and CSS files used by your HTML file. It also includes a development server that refreshes the page when you save a change.
This example assumes an index.html entry file in the project directory. Install Parcel as a development dependency, add scripts to package.json, and run the project:
# Step 1: Install Parcel as a development dependency
npm install --save-dev parcel
# Step 2: Add these scripts to package.json
# "scripts": {
# "start": "parcel index.html",
# "build": "parcel build index.html"
# }
# Step 3: Start the development server
npm run start
# Step 4: Build for production
npm run build
During development, Parcel serves a local URL, usually http://localhost:1234, and updates it when files change. The production command writes prepared files to a dist folder.
The development server is intended for local development. The production build generates optimized files that can be deployed to a web server or hosting platform. You should deploy the generated production output rather than use the development server for production hosting.
Parcel can transform some files for you, but browser targets and project settings still decide which syntax changes and polyfills are included.
When to use Parcel: If you want to get up and running quickly without fussing over configuration, Parcel is a great starting point. For large-scale projects with very specific needs, Webpack or Vite might give you more control.JavaScript keeps getting better. Every year new features are added to the language. But browsers take time to support them. Some users are on older browsers. Some are stuck on corporate machines that have not been updated in years.
So what do you do when you want to use modern JavaScript features, but you need your code to work on older browsers too?
Babel is a JavaScript compiler that can transform modern JavaScript syntax into syntax supported by older browsers or JavaScript runtimes. It is commonly used when a project needs to support environments that do not understand certain language features.
Babel does not automatically make every modern JavaScript API available in older browsers. Syntax transformation and API polyfilling are separate concerns, which we will cover later.
Here is a simple example. You write this modern code using an arrow function and a template literal:
// Modern JavaScript (ES6+)
const greet = (name) => `Hello, ${name}!`;
console.log(greet("Developer"));
Babel converts it to something like this, which older browsers can run:
// After Babel (ES5 compatible)
"use strict";
var greet = function greet(name) {
return "Hello, " + name + "!";
};
console.log(greet("Developer"));
The output does the same job using older syntax.
Babel uses plugins for individual transformations and presets for groups of transformations. @babel/preset-env helps choose transformations for the browsers you support.
npm install --save-dev @babel/core@7 @babel/preset-env@7
# For Webpack integration only:
npm install --save-dev babel-loader
babel-loader connects Babel with Webpack. It is not required when Babel is used independently or with a different build tool. The integration depends on the bundler used by the project. The configuration below illustrates Babel 7.
You then create a .babelrc file (or add a babel section in package.json) to configure it:
{
"presets": [
["@babel/preset-env", {
"targets": "> 0.25%, not dead"
}]
]
}
The targets option tells Babel which browsers or JavaScript environments the project needs to support. Babel uses this information to decide which syntax transformations are necessary. Choose targets based on your application's actual browser-support requirements rather than copying a target query blindly.
Transpiling is a form of source-to-source compilation. The input code is converted into another form of source code, often to improve compatibility or prepare it for another tool. In JavaScript projects, transpiling can also be used to convert TypeScript or JSX into JavaScript.
For example, Babel can change an arrow function into a regular function. The code is still JavaScript, but it uses syntax that older browsers understand.
The earlier Babel example shows this process. Here are a few more examples:
// Modern: Destructuring
const { name, age } = person;
// After transpiling:
var name = person.name;
var age = person.age;
// -------------------------------------
// Modern: Default parameters
function greet(name = "World") {
return "Hello, " + name;
}
// After transpiling:
function greet() {
var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "World";
return "Hello, " + name;
}
// -------------------------------------
// Modern: Spread operator
const arr = [1, 2, 3];
const copy = [...arr];
// After transpiling:
var arr = [1, 2, 3];
var copy = arr.slice();
Similarly, stylesheet preprocessors convert SCSS or Less into standard CSS.
Tip: You can try Babel's transpiling right in your browser at babeljs.io/repl. Paste in modern JavaScript on the left and see the ES5 output on the right. It is a great way to understand exactly what Babel does.Babel can convert new syntax to older syntax. But some things are not syntax - they are brand new built-in features that simply do not exist in older browsers at all.
For example, Array.prototype.includes() was added in ES7 (2016). Older browsers do not have it. Babel cannot "transpile" it away because there is no older syntax equivalent. It is a missing feature.
That is where polyfills come in. A polyfill is a piece of code that adds an implementation for a missing API or feature when that is possible. That is different from transforming syntax.
A polyfill adds code that provides a feature when a browser does not support that feature.
The following is a simplified educational example to demonstrate how a polyfill works. It does not implement every detail of the official Array.prototype.includes() specification. In production applications, use a maintained polyfill library when broader browser compatibility is required. For example, it does not support the optional fromIndex argument.
// Simplified educational polyfill for Array.prototype.includes
if (!Array.prototype.includes) {
Array.prototype.includes = function (value) {
for (var i = 0; i < this.length; i++) {
var item = this[i];
if (
item === value ||
(item !== item && value !== value)
) {
return true;
}
}
return false;
};
}
var roles = ["admin", "editor", "viewer"];
console.log(roles.includes("editor")); // true
console.log(roles.includes("guest")); // false
You would usually use a library instead of writing polyfills by hand. core-js is a common polyfill library. With @babel/preset-env, you can configure Babel to include polyfills for the browsers you support:
core-js provides implementations of many JavaScript built-in features for environments that do not support them natively. The exact polyfills included depend on the configuration and the features used by the application. The following configuration illustrates Babel 7 with core-js 3.
{
"presets": [
["@babel/preset-env", {
"targets": "> 0.25%, not dead",
"useBuiltIns": "usage",
"corejs": 3
}]
]
}
In Babel 7, useBuiltIns: "usage" adds imports for the core-js polyfills needed by the features in your code and the environments you target. Install core-js as a runtime dependency and configure its installed version, including the minor version when you know it. Babel 8 removed these preset options; it uses babel-plugin-polyfill-corejs3 instead. Check the Babel documentation for your version.
To summarise the difference:
- Transpiling - converts new syntax into old syntax (Babel does this)
- Polyfilling - adds missing features/methods that do not exist yet in the browser (core-js does this)
- Node.js provides the JavaScript runtime, while npm helps install and manage project dependencies.
- package.json stores project metadata, scripts, dependencies and development dependencies.
- Lock files record resolved dependency versions and help maintain consistent installations across environments.
- npm, Yarn and pnpm are popular package managers. A project should normally use one consistently.
- Bundlers process project dependencies and prepare optimized files for deployment.
- Webpack, Parcel and Rollup are bundlers, while Vite provides a modern development and build workflow.
- ESLint identifies potential code problems and enforces coding rules, while Prettier formats code consistently.
- Babel transforms modern JavaScript syntax to support the environments specified by a project.
- Transpiling converts source code into another form of source code.
- Polyfills provide implementations for missing JavaScript APIs when possible.
- Modern frameworks and development tools often configure many of these tools for you, so you can focus on building applications.
What's next? Now that you understand the tools developers use to ship JavaScript, check out Testing JavaScript to learn how to make sure your code actually works.
- What does a package manager do in a JavaScript project?
- How does a lockfile differ from package.json?
- What is the browser console useful for?
- What are browser developer tools?
- What does a code formatter do?
- How does a linter differ from a formatter?
- How do source maps help with bundled applications?
- What can a performance profile reveal?
- What can the browser Network panel diagnose?
- What does a JavaScript package manager do?
- What problem does a JavaScript bundler solve?
- What does tree shaking depend on?
- Why commit a dependency lockfile?
- What makes a build reproducible?
- How can teams reduce dependency supply-chain risk?