この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:6
ページ更新者:T
更新日時:2026-06-11 07:12:00

タイトル: クイックスタート
SEOタイトル: Node.js クイックスタート — インストールから Hello World / Express API まで

この記事の要点
  • インストールは nvm (推奨) または公式インストーラー / Homebrew / winget / Volta
  • node -v でバージョン確認 → console.log() で Hello World
  • http.createServer で最小 HTTP サーバ → npm install express でルーティング
  • npm init -ypackage.json 生成 → scriptsnpm start/dev/test
  • .gitignorenode_modules / .env を必ず追加

1. Node.js のインストール

nvm を使う方法 (Linux / macOS 推奨)

# nvm をインストール
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
# シェル再起動 or
source ~/.bashrc

# 最新 LTS をインストール
nvm install --lts
nvm use --lts
nvm alias default lts/*

# 確認
node -v   # v22.x.x
npm -v    # 10.x.x

Windows (nvm-windows / winget)

# winget で公式版
winget install OpenJS.NodeJS.LTS

# または nvm-windows
# https://github.com/coreybutler/nvm-windows/releases
nvm install lts
nvm use lts

# 確認
node -v
npm -v

macOS (Homebrew)

brew install node

# または特定バージョン
brew install node@22
brew link node@22

# Volta (バージョン自動切替に便利)
curl https://get.volta.sh | bash
volta install node@lts

2. 初めての Hello World

// hello.js
console.log('Hello, Node.js!');
console.log('Node version:', process.version);
console.log('Platform:', process.platform);

実行:

node hello.js
# Hello, Node.js!
# Node version: v22.0.0
# Platform: linux

3. REPL (対話シェル)

# 対話モードに入る
node

> 1 + 1
2
> const x = [1, 2, 3]
> x.map(n => n * 2)
[ 2, 4, 6 ]
> .exit

4. 最小 HTTP サーバ (素の Node)

// server.js
import http from 'node:http';

const server = http.createServer((req, res) => {
  console.log(`${req.method} ${req.url}`);
  res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
  res.end('Hello from Node!\n');
});

const PORT = 3000;
server.listen(PORT, () => {
  console.log(`Listening on http://localhost:${PORT}`);
});

実行 → ブラウザで http://localhost:3000

node server.js
# 別ターミナルから
curl http://localhost:3000
# Hello from Node!

5. npm init + Express で API サーバ

プロジェクト初期化

mkdir my-api && cd my-api
npm init -y                  # package.json 生成 (-y で全デフォルト)
npm install express          # Express 追加
npm install -D nodemon       # ファイル変更で自動再起動

package.json の例

{
  "name": "my-api",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js",
    "test": "node --test"
  },
  "dependencies": {
    "express": "^4.19.0"
  },
  "devDependencies": {
    "nodemon": "^3.1.0"
  }
}

index.js

import express from 'express';

const app = express();
app.use(express.json());

// 一覧
let todos = [{ id: 1, text: 'Learn Node' }];

app.get('/todos', (req, res) => {
  res.json(todos);
});

// 作成
app.post('/todos', (req, res) => {
  const todo = { id: Date.now(), text: req.body.text };
  todos.push(todo);
  res.status(201).json(todo);
});

// 削除
app.delete('/todos/:id', (req, res) => {
  todos = todos.filter(t => t.id !== Number(req.params.id));
  res.status(204).end();
});

app.listen(3000, () => console.log('API on :3000'));

起動

npm run dev               # nodemon で自動再起動
# 別ターミナルで動作確認
curl http://localhost:3000/todos
curl -X POST http://localhost:3000/todos \
     -H "Content-Type: application/json" \
     -d '{"text":"Buy milk"}'

6. package.json scripts の標準コマンド

コマンド慣習的な用途
npm start本番モードで起動 (node index.js)
npm run dev開発モード (nodemon / tsx watch)
npm run build本番ビルド (tsc / vite build)
npm testテスト実行 (node --test / jest / vitest)
npm run lintESLint 実行
npm run formatPrettier 実行

7. .gitignore 必須項目

# 依存
node_modules/

# 環境変数 (絶対コミット禁止)
.env
.env.local
.env.production

# ビルド成果物
dist/
build/
.next/
.nuxt/
.svelte-kit/

# ログ
*.log
npm-debug.log*
yarn-debug.log*
pnpm-debug.log*

# エディタ
.vscode/
.idea/
.DS_Store

node_modules は git にコミットしないのが鉄則。サイズが大きく、package.json + npm install で復元できるためです。

8. TypeScript で始めたい場合

npm install -D typescript tsx @types/node @types/express
npx tsc --init

# 開発実行
npx tsx watch src/index.ts

# ビルド → 本番起動
npx tsc
node dist/index.js

よくある詰まりポイント

症状原因対処
command not found: nodePATH に通っていないnvm 設定 / インストーラ再実行
EADDRINUSEポート 3000 使用中別ポート / プロセス kill
SyntaxError: Cannot use import statement outside a modulepackage.json に "type": "module"追加 or 拡張子 .mjs
Error: Cannot find module 'express'npm install 忘れnpm install
npm WARN deprecated 多発古い依存npm audit fix / npm update

FAQ

Q: nvm と Volta どちらが良い?
A: シンプルなら nvm。プロジェクトごとに自動切替したいなら Volta (package.json にバージョンを記録)。

Q: yarn / pnpm に切り替えるべき?
A: 速度・ディスク節約なら pnpm が現状最強。npm 標準で困らなければそのままで OK。

Q: index.js / server.js / app.js どれを起点にすべき?
A: 慣習はあるが厳密ルールはなし。package.jsonmain または scripts.start を見れば分かるので統一しましょう。