できない.dev

TypeScript「Cannot find name 'process'」の原因と直し方

Cannot find name 'process' は、@types/node を入れて tsconfig の types に "node" を足せば直る。
TypeScript 6.0 は types の既定が空になり、@types/node を入れても読まない。

公開:

症状できない · exit 2
npx tsc -p .
src/index.ts(1,30): error TS2591: Cannot find name 'fs'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(3,14): error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(4,19): error TS2304: Cannot find name '__dirname'.
解決後できた · exit 0
npx tsc -p .

2026-11-07 に node:24(TypeScript 6.0.3 / Node.js 24.22.2) で実際に打って取った出力。検証環境の詳細

要約

error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? は、Node.js の型定義(@types/node)が読まれていないときに出るエラーだ。
process・require・Buffer のように Node.js がグローバルに置く名前で止まり、6.0 では fs のような組み込みモジュールの import も同じ形になる。
__dirname は案内の付かない TS2304: Cannot find name '__dirname'. になる。

TypeScript 6.0 では、tsconfig.json の types の既定が [] になった。
エラーの文言のとおり npm i --save-dev @types/node を打っても、types に "node" を書くまで同じエラーが続く。

実行例

node:24 のコンテナで、types を書いていない tsconfig.json を TypeScript 6.0.3 の tsc にかけた。
@types/node を入れたあとも同じ 3 つのエラーが残り、types に "node" を足すと終了コードが 0 になった。

$ npx tsc -v
Version 6.0.3
$ cat src/index.ts
import { readFileSync } from "fs";
 
const home = process.env.HOME;
console.log(home, __dirname, readFileSync("package.json", "utf8").length);
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true
  },
  "include": ["src"]
}
$ npx tsc -p .
src/index.ts(1,30): error TS2591: Cannot find name 'fs'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(3,14): error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(4,19): error TS2304: Cannot find name '__dirname'.
$ echo $?
2
$ npm install --save-dev @types/node@24
added 2 packages in 761ms
$ npx tsc -p .
src/index.ts(1,30): error TS2591: Cannot find name 'fs'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(3,14): error TS2591: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node` and then add 'node' to the types field in your tsconfig.
src/index.ts(4,19): error TS2304: Cannot find name '__dirname'.
$ echo $?
2
$ cat tsconfig.json
{
  "compilerOptions": {
    "noEmit": true,
    "types": ["node"]
  },
  "include": ["src"]
}
$ npx tsc -p .
$ echo $?
0
$ npx tsc -v
Version 5.9.3
$ npx tsc -p .
src/index.ts(1,30): error TS2307: Cannot find module 'fs' or its corresponding type declarations.
src/index.ts(3,14): error TS2580: Cannot find name 'process'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.
src/index.ts(4,19): error TS2304: Cannot find name '__dirname'.
$ echo $?
2

— 2026-11-07 時点の出力

検証環境Node.js 24.22.2 / npm 11.19.1 / Python 3.11.2 / 2026-11-07 検証(ほか 2 件)
検証日
実行環境
node:24 Debian GNU/Linux 12 (bookworm)
バージョン
  • Node.js 24.22.2
  • npm 11.19.1
  • Python 3.11.2
  • Git 2.39.5
  • TypeScript 6.0.3

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. @types/node を入れて types に node を足す

npm install --save-dev @types/node
{
  "compilerOptions": {
    "types": ["node"]
  }
}

両方が要る。
@types/node を入れずに types だけを書くと、今度は TS2688: Cannot find type definition file for 'node'. になる。
@types/node の major は、動かす Node.js の版に揃える(Node.js 24 なら @types/node@24)。

2. ほかの型定義も types に並べる

types は書いた名前だけを読むので、Jest のテストも同じ tsconfig.json で検査するなら ["node", "jest"] と並べる(describe の記事)。
5.x と同じく node_modules/@types を全部読ませるなら、"types": ["*"] と書く。

lodash のように名前を書いて import するパッケージの型は、types に並べなくても読まれる。
types が決めるのは、import しなくても使えるグローバルな名前を足す型定義だけである。

3. ブラウザ向けのコードは import.meta.env で読む

Vite でまとめるフロントエンドのコードでは、@types/node を足さずに import.meta.env.VITE_API_URL のように読む(Vite の環境変数(新しいタブで開く))。
tsconfig.json の types には "vite/client" を入れる。
Node.js で動く設定ファイル(vite.config.ts)だけは @types/node が要るので、そのファイルを検査する tsconfig.json に "node" を足す。

よくある原因

  1. TypeScript を 6.0 に上げた。
    5.x は types を書かなければ node_modules/@types の型定義を全部読んでいたので、@types/node を入れるだけで通っていた。
    6.0 は types に書いた名前しか読まない。
  2. @types/node を入れていない。
    5.x では process が TS2580 になり、文言は Try \npm i --save-dev @types/node`. で終わる。fsのimportはTS2307: Cannot find module 'fs'になる([Cannot find module の記事](/typescript/cannot-find-module-or-types/))。6.0 ではどちらもTS2591 になり、types` に足すよう促す一文が後ろに付く。
  3. types に別の名前だけを並べている。
    "types": ["jest"] と書くと、そこに無い node は読まれない。
    5.x でも types を書いていれば、6.0 と同じ TS2591 になる。
  4. ブラウザで動くコードで process.env.API_URL のように環境変数を読んでいる。
    ブラウザに process は無いので、型定義を足しても実行時には動かない。