Setting up Node.js development environment in Windows
· 阅读需 4 分钟
1. Install Node.js
- Download Node.js from the official website. Use the LTS version for stability.
- Install Node.js by running the installer and ensure you check the box to install npm (Node Package Manager).
- Verify the installation:
node -vnpm -v
2. Install Yarn (Optional)
While npm is included with Node.js, Yarn is a popular alternative for managing dependencies.
- Install Yarn globally:
npm install --global yarn
- Verify the installation:
yarn -v
3. Install Git
- Download and install Git from git-scm.com.
- During installation, choose "Git Bash Here" and "Use Git from the Command Prompt."
- Verify Git installation:
git --version
4. Install Visual Studio Code
- Download and install VS Code.
- Install the following extensions in VS Code:
- ESLint: Linting and code quality.
- Prettier: Code formatting.
- TypeScript: Provides TypeScript language features.
- React Developer Tools: Debugging React apps.
- REST Client: For testing APIs.
5. Setup Node.js + TypeScript Project
Step 1: Initialize the Project
- Create a new folder for your project:
mkdir my-node-react-appcd my-node-react-app
- Initialize the project with Yarn or npm:
yarn init -y# ORnpm init -y
Step 2: Install TypeScript
- Install TypeScript and Node.js types as dev dependencies:
yarn add -D typescript @types/node
- Create a
tsconfig.json:npx tsc --init
Example tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "Node",
"resolveJsonModule": true,
"outDir": "./dist",
"rootDir": "./src",
"baseUrl": "./",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
Step 3: Setup Front-End (React + Vite)
- Create the React front-end:
yarn create vite react-frontend --template react-ts
- Navigate to the front-end folder:
cd react-frontend
- Install dependencies for React development:
yarn add react react-domyarn add -D @types/react @types/react-dom eslint prettier
- Install additional libraries:
yarn add axios zustand zod clsx @tanstack/react-query
Step 4: Setup Back-End (Node.js)
- Navigate back to the root directory.
- Create a folder for the back-end:
mkdir backendcd backend
- Install Express and related types:
yarn add expressyarn add -D @types/express nodemon
- Create a
srcfolder and start building your API:mkdir srctouch src/index.ts
Example src/index.ts:
import express from 'express';
const app = express();
app.use(express.json());
app.get('/api', (req, res) => {
res.send('Hello from the back-end!');
});
const PORT = 3001;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
- Add a
nodemonscript to automatically restart the server during development:"scripts": {"dev": "nodemon src/index.ts"}
Step 5: Configure Path Aliases
-
Add the following to both
react-frontendandbackendprojects'tsconfig.json:"paths": {"@/*": ["src/*"]} -
In Vite's
vite.config.tsfor the front-end, install and configurevite-tsconfig-paths:yarn add -D vite-tsconfig-pathsvite.config.ts:
import { defineConfig } from 'vite';import react from '@vitejs/plugin-react';import tsconfigPaths from 'vite-tsconfig-paths';export default defineConfig({plugins: [react(), tsconfigPaths()],});
Step 6: Setup Docker
- Create a
Dockerfilein the root directory for both front-end and back-end.
Front-End Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
CMD ["yarn", "dev"]
Back-End Dockerfile:
FROM node:18-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install
COPY . .
CMD ["yarn", "dev"]
- Create a
docker-compose.yml:
version: '3.8'
services:
frontend:
build:
context: ./react-frontend
ports:
- "5173:5173"
backend:
build:
context: ./backend
ports:
- "3001:3001"
Step 7: Debugging in VS Code
-
Install the Remote - Containers extension in VS Code.
-
Create a
.devcontainerfolder in the root directory. -
Add a
devcontainer.json:{"name": "Node.js Dev","dockerComposeFile": "docker-compose.yml","service": "frontend","workspaceFolder": "/app","extensions": ["dbaeumer.vscode-eslint","esbenp.prettier-vscode"],"settings": {"terminal.integrated.shell.linux": "/bin/bash"}} -
Reopen the project in the container using the Command Palette (
Ctrl + Shift + P).
8. Start Development
- Start the Docker containers:
docker-compose up --build
- Access the front-end at
http://localhost:5173and the back-end athttp://localhost:3001.
Project Structure
my-node-react-app/
├── backend/
│ ├── src/
│ │ └── index.ts
│ ├── package.json
│ └── tsconfig.json
├── react-frontend/
│ ├── src/
│ │ ├── App.tsx
│ │ ├── main.tsx
│ │ └── ...
│ ├── package.json
│ ├── tsconfig.json
│ └── vite.config.ts
├── docker-compose.yml
└── .devcontainer/
└── devcontainer.json