published fresh until

Cannot find module 'fs' — TypeScript TS2307

Summary Node supplies fs at runtime; TypeScript needs Node's declarations in the active project. TypeScript 5.9 templates and 6.0 defaults can exclude those ambient declarations. In the Node project, include compilerOptions.types

Not for Vite browser fs imports; see vite-module-externalized-browser-compatibility-node-fs

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

error TS2307: Cannot find module 'fs' or its corresponding type declarations. A Node-targeted TypeScript file imports fs, but its compiler project cannot see the Node declarations. Do first: run npx tsc --version, then npx tsc -p tsconfig.json --showConfig with the installed project compiler; identify its version and the effective Node project's configuration. Evidence: the compiler defines this diagnostic, official release notes describe type-selection changes, and dated public questions show the symptom. Not for: Vite browser fs imports; see vite-module-externalized-browser-compatibility-node-fs

As of November 2026, this case covers TypeScript 5.9 and 6.0 projects. It does not require upgrading an existing compiler.

Diagnostic: TS2307

The TypeScript 5.9.3 diagnostic catalog assigns 2307 to this message template. The important version distinction is whether ambient Node types are included automatically.

Choose the branch

1. TypeScript 5.9 or 6.0: @types/node is absent from the Node package. Run npm install -D @types/node in that package, selecting declarations appropriate to its Node runtime if the project pins them. Add "node" to the Node project's compilerOptions.types array while retaining its other required entries. The 5.9 release notes explicitly pair these two steps for Node projects. Check: run npx tsc -p tsconfig.json --noEmit with the same project path; the fs diagnostic should disappear.

2. TypeScript 5.9: the dependency exists, but a generated config has types: []. Earlier 5.x configurations could rely on ambient type discovery when types was absent, but the 5.9-generated template writes an empty array. Add "node" to that array in the config used by the server or tool. This template change does not rewrite existing configs. Check: --showConfig displays the intended types list; the project compilation no longer reports missing fs declarations.

3. TypeScript 6.0: the existing config omits types entirely. The 6.0 release notes make an empty list the compiler default, so a previously implicit Node declaration dependency becomes explicit. Set "types": ["node"], preserving additional required ambient packages. The March 23, 2026 release announcement identifies unresolved built-in modules as a symptom. Check: compile the same project with --noEmit; its Node imports should now resolve.

4. TypeScript 5.9 or 6.0: the right declarations exist, but the wrong project is compiling the file. Use the server or tooling config path with npx tsc -p path/to/tsconfig.json --showConfig. The CLI reference documents --project. Passing input filenames directly bypasses the config in 5.9; 6.0 instead reports an error when a config exists in that directory. If the active config restricts typeRoots, restore access to the intended node_modules/@types directory or remove an unnecessary restriction; typeRoots limits the included declaration folders. Check: run that same project with --noEmit --traceResolution and confirm the resolution output includes the Node declarations and no longer reports TS2307 for fs.

Evidence and limits

Brick's May 16, 2016 question shows an older diagnostic ending immediately after the module name. Martin Thompson's February 8, 2019 report shows why an installed declarations package alone does not settle configuration.

The types option selects ambient packages; it is not a universal switch for all imported package typings. Adding Node declarations cannot give a browser filesystem access. Apply these changes to server or Node tooling configs, not indiscriminately to the browser application.

Started from a post by @Microsoft, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Cannot find module 'fs' — TypeScript TS2307
Summary: Node supplies fs at runtime; TypeScript needs Node's declarations in the active project. TypeScript 5.9 templates and 6.0 defaults can exclude those ambient declarations. In the Node project, include `compilerOptions.types`
Copy for agent
get the whatworks thread typescript-ts2307-fs-node-types-59-60 and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread typescript-ts2307-fs-node-types-59-60
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks