Skip to contentSkip to navigationSkip to topbar
Rate this Page:

How to set up your Node.js and Express development environment


In this guide, we'll cover how to set up your Node.js development environment for an Express(link takes you to an external page) project. We'll also walk through some helpful tools that we recommend for all Node.js applications that use Twilio: ngrok and the Twilio Node.js SDK(link takes you to an external page)


Install Node.js

install-nodejs page anchor

How you install Node.js varies depending on your operating system.

Operating SystemInstructions
OS XThe easiest way to install Node.js on OS X is to use the official installer from nodejs.org(link takes you to an external page). You can also use Homebrew(link takes you to an external page) if you prefer. To manage and switch between versions of Node.js on your machine, we recommend using nvm(link takes you to an external page).
WindowsThe easiest way to install Node.js on Windows is the official installer from nodejs.org(link takes you to an external page). You can also use Chocolatey(link takes you to an external page) if you prefer. To manage and switch between versions of Node.js on your machine, we recommend using nvm-windows(link takes you to an external page).
LinuxThe Node.js installation method(link takes you to an external page) varies by distribution. To manage and switch between versions of Node.js on your machine, we recommend using nvm(link takes you to an external page).

Install a text editor or IDE

install-a-text-editor-or-ide page anchor

Before we can start a Node.js project, we'll need a place to write our code.

If you already have a code-writing tool of choice, you can stick with it for developing your Node.js application. If you're looking for something new, we recommend trying out a few options:

If you're new to programming, we highly recommend getting off to a good start with Visual Studio Code. Many developers here at Twilio and in the wider JavaScript ecosystem are extremely happy using it.


Start a new Node.js project with npm init

start-a-new-nodejs-project-with-npm-init page anchor

Before starting any new Node.js project we should run npm init to create a new package.json file for our project.

Create a new empty directory in your development environment and run npm init. You'll then answer a few basic questions about your project, and npm will create a new package.json file for you when you're done.


_36
$ npm init
_36
This utility will walk you through creating a package.json file.
_36
It only covers the most common items, and tries to guess sensible defaults.
_36
_36
See `npm help init` for definitive documentation on these fields
_36
and exactly what they do.
_36
_36
Use `npm install <pkg>` afterward to install a package and
_36
save it as a dependency in the package.json file.
_36
_36
Press ^C at any time to quit.
_36
package name: (my-project)
_36
version: (1.0.0)
_36
description: A sample Twilio project
_36
entry point: (index.js)
_36
test command:
_36
git repository:
_36
keywords:
_36
author: Jane Doe
_36
license: (ISC)
_36
About to write to /Users/<your-username>/my-project/package.json:
_36
_36
{
_36
"name": "my-project",
_36
"version": "1.0.0",
_36
"description": "A sample Twilio project",
_36
"main": "index.js",
_36
"scripts": {
_36
"test": "echo \"Error: no test specified\" && exit 1"
_36
},
_36
"author": "Jane Doe",
_36
"license": "ISC"
_36
}
_36
_36
_36
Is this OK? (yes) yes

Now we're ready to install our Node.js dependencies.

(information)

Info

You can quickly initialize your project and skip the above prompts by running npm init -y


Install Express.js and the Twilio Node.js SDK

install-expressjs-and-the-twilio-nodejs-sdk page anchor

We're almost ready to write an Express web application, but first, we need to install the Express package using npm.


_10
# Use npm to install the express and Twilio packages
_10
$ npm install express twilio
_10
# List the installed dependencies and their versions
_10
$ npm ls
_10
my-project@1.0.0 /Users/<your-username>/my-project
_10
├── express@4.17.1
_10
└── twilio@3.67.2

Node.js uses npm(link takes you to an external page) to manage dependencies, so the command to install Express and the Twilio SDK to our development environment is npm install express twilio.

Installing these packages tells npm to add the Express and Twilio packages to the dependencies object in our project's package.json file. When we want to install these same packages again in the future - like on a production server - we can just run npm install.


Create a simple Express.js application

create-a-simple-expressjs-application page anchor

We can test that we configured our development environment correctly by creating a simple Express application. We'll grab the ten-line example from Express's documentation and drop it in a new file called index.js.


_11
const express = require('express');
_11
const app = express();
_11
const port = 3000;
_11
_11
app.get('/', (req, res) => {
_11
res.send('Hello World!');
_11
});
_11
_11
app.listen(port, () => {
_11
console.log(`Example app listening at http://localhost:${port}`);
_11
});

We can then try running our new Express application with the command node index.js. If you open http://localhost:3000(link takes you to an external page) in your browser, you should see the "Hello World!" response.

(warning)

Warning

If you're using a virtual machine for your development environment, like Vagrant(link takes you to an external page), you might not see your Express application at the localhost hostname. Continue to the ngrok section for an easy way to fix this.


Install ngrok for local development

install-ngrok-for-local-development page anchor

Once you see your sample Express application's "Hello World!" message, your development environment is ready to go. However, for most Twilio projects you'll want to install one more helpful tool: ngrok(link takes you to an external page).

Most Twilio services use webhooks(link takes you to an external page) to communicate with your application. When Twilio receives an incoming phone call, for example, it reaches out to a URL in your application for instructions on how to handle the call.

When you're working on your Express application in your development environment, your app is only reachable by other programs on the same computer, so Twilio won't be able to talk to it.

Ngrok is our favorite tool for solving this problem. Once started, it provides a unique URL on the ngrok.io domain which will forward incoming requests to your local development environment.

To start, head over to the ngrok download page and grab the binary for your operating system: https://ngrok.com/download(link takes you to an external page)

Once downloaded, make sure your Express application is running, and then start ngrok using the command ./ngrok http 3000. You should see output similar to this:


_12
ngrok by @inconshreveable (Ctrl+C to quit)
_12
_12
Session Status online
_12
Account <Your name> (Plan: Free)
_12
Version 2.3.40
_12
Region United States (us)
_12
Web Interface http://127.0.0.1:4040
_12
Forwarding http://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:3000
_12
Forwarding https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io -> http://localhost:3000
_12
_12
Connections ttl opn rt1 rt5 p50 p90
_12
0 0 0.00 0.00 0.00 0.00

Your unique ngrok domain name will be visible on the "Forwarding" line. Here, ours is "https://6e81-2601-1c0-6100-5087-309b-c292-5e5f-1f.ngrok.io(link takes you to an external page)".

If everything is working correctly, you should be able to open that domain name in your browser and see your Express application's "Hello World!" message displayed at your new ngrok URL.

Anytime you're working on your Twilio application and need a URL for a webhook, use ngrok to get a publicly accessible URL like this one.


Where to next with Express and Node?

where-to-next-with-express-and-node page anchor

You're now ready to build out your Express application! Here are a few other sample applications we've built:

More Node.js and Express resources and guides

more-nodejs-and-express-resources-and-guides page anchor

Rate this Page: