Fix 'Cannot find module fs' in VS Code

By

How to fix the Cannot find module fs error in VS Code by installing the @types/node package as a dev dependency and reloading the editor window.

~~~

Got this error in VS Code?

Cannot find module 'fs' or its corresponding type declarations.ts(2307)

This page covers errors on imports of Node.js built-ins. For an error on tsconfig.json itself or an unknown global like process, see the Node type-definition configuration guide.

Install the declarations for Node’s built-in modules:

npm install --save-dev @types/node

and reload the VS Code window.

To reload, open the command palette (cmd-shift-P on Mac, ctrl-shift-P on Windows) and run “Developer: Reload Window”. Sometimes “TypeScript: Restart TS Server” is enough.

Why does this error happen?

Your code is fine. The fs module exists, and the program runs.

The problem is on the editor side. VS Code uses TypeScript to analyze your code, and TypeScript needs type declarations to know what a module exports. Node.js built-ins like fs, path, and http don’t ship with declarations, they only exist at runtime.

The @types/node package fills that gap. It describes every built-in Node module, so the editor knows that fs exists and what readFile() looks like.

That’s why the same error shows up with any built-in:

Cannot find module 'path' or its corresponding type declarations.ts(2307)

One install supplies declarations for all these built-ins.

This happens in plain JavaScript projects too, not just TypeScript ones. VS Code runs the same analysis on .js files to power autocomplete.

Still broken? Check the project VS Code opened

Open the folder containing your package.json and install the package there. In a workspace with several packages, check that the file belongs to the project where you installed it.

You can also use the node: prefix in imports to make the built-in module explicit:

import fs from 'node:fs'
import path from 'node:path'

The prefix identifies the built-in module; you still need its type declarations.

A types setting controls which declarations enter the global scope. It does not generally disable typing for imported modules, as the TypeScript documentation explains. For types, custom typeRoots, and monorepo configuration checks, follow the Node type-definition guide.

One more tip

@types/node is versioned to match Node.js releases. If you’re on Node 24, you can pin the matching declarations:

npm install --save-dev @types/node@24

This keeps the editor from suggesting APIs that only exist in newer Node versions than the one you run.

Want me to talk about your product? You can sponsor this site.

~~~

Related posts about node: