Local Development Environment Setup
This guide explains how to set up a local development environment for one of our projects. If this is your first time setting up your machine, please follow the Developer Machine Setup Guide first.
Our projects typically use OpenNext and are deployed on Cloudflare.
Prerequisites
Before you begin, make sure you have the following installed:
- Git: For version control.
- Node.js: We recommend using a version manager like nvm to easily switch between Node.js versions. Check the project’s
.nvmrcorpackage.jsonfile for the required version. - npm or yarn: Our projects use
npm, which is included with Node.js. - A code editor: We recommend Visual Studio Code (VS Code).
Initial Setup (for a new project)
-
Clone the Repository:
git clone [repository-url] cd [project-directory] -
Install the Correct Node.js Version (if using nvm):
nvm install nvm use -
Install Dependencies:
npm install -
Set up Environment Variables:
- Our projects use a
.envfile for local environment variables. See our guide on Managing Secrets in Cloudflare for more context on how we handle secrets across environments. - Find the
.env.examplefile in the root of the project. - Copy it to a new file named
.env:cp .env.example .env - Fill in the required values in the
.envfile. For local development, these will often be mock values or credentials for a local database or service. Ask a team member if you are unsure what to use. - Important: The
.envfile should never be committed to Git.
- Our projects use a
-
Run the Development Server:
- The command to start the local development server is usually in the
package.jsonfile. -
npm run dev - This will start the application, typically at
http://localhost:3000. The server will automatically reload when you make changes to the code.
- The command to start the local development server is usually in the
Simulating the Cloudflare Environment Locally
- OpenNext: The OpenNext library is designed to work seamlessly with Next.js, and for local development, the standard
next devcommand is usually sufficient. - Wrangler (for advanced use cases): Cloudflare’s command-line tool, Wrangler, can be used to emulate the Cloudflare environment more closely. For projects that rely heavily on specific Cloudflare features (like Workers, KV stores, or R2), we may use Wrangler for local development.
- To run a project with Wrangler locally:
npx wrangler dev - This requires a
wrangler.tomlconfiguration file. Refer to the project’sREADME.mdfor specific instructions.
- To run a project with Wrangler locally:
Recommended VS Code Extensions
To ensure a consistent development experience, we recommend installing the following VS Code extensions:
- ESLint: Integrates ESLint into your editor.
- Prettier - Code formatter: Automatically formats your code on save.
- EditorConfig for VS Code: Helps maintain consistent coding styles across different editors.
- GitLens: Supercharges the Git capabilities built into VS Code.
- DotENV: Provides syntax highlighting for
.envfiles.