Cannot find module 'fs' or 'path' Error in TypeScript [Fix]

avatar
Borislav Hadzhiev

Last updated: Feb 27, 2024
5 min

banner

# Table of Contents

  1. Cannot find module 'fs' Error in TypeScript
  2. Cannot find module 'fs/promises' Error in TypeScript
  3. Cannot find module 'path' Error in TypeScript

If you got the "Cannot find module 'path'" error, click on the third subheading.

# Cannot find module 'fs' Error in TypeScript

To solve the "Cannot find module fs or its corresponding type declarations" error, install the types for Node by running the command npm i -D @types/node.

You can then import fs with the following line of code import * as fs from 'fs'.

typescript cannot find module fs

# Install the typings for Node

Make sure to install the typings for Node by opening your terminal in your project's root directory and running the following command.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install -D @types/node # ๐Ÿ‘‡๏ธ or with YARN yarn add @types/node --dev

This will install the typings for Node as a development dependency in your project.

Now you can import the fs module with the following line of code.

index.ts
import * as fs from 'fs'; console.log(fs);

# Add node to the types array in your tsconfig.json file

If your error has not been resolved, open your tsconfig.json file and make sure the types array contains the string node.

tsconfig.json
{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the fs module.

# Delete your node_modules and reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

Make sure to restart your IDE if the error persists.

# Reading a file with the fs module in a TypeScript project

Here is an example of how you would read a file in the same directory named another-file.ts with the fs module using TypeScript.

index.ts
import * as fs from 'fs'; import * as path from 'path'; console.log( fs.readFileSync(path.join(__dirname, './another-file.ts'), { encoding: 'utf-8', }), );

And here is the output in my terminal showing the contents of another-file.ts.

fs reading file success

I've also written a detailed guide on how to import and use the fs module in TypeScript.

# Cannot find module 'fs/promises' Error in TypeScript

To solve the "Cannot find module fs/promises or its corresponding type declarations" error, install the types for Node by running the command npm i -D @types/node.

cannot find module fs promises

Make sure to install the typings for Node, by opening your terminal in your project's root directory and then run the following command:

shell
npm i -D @types/node

This will install the typings for Node as a dev dependency in your project.

Now you are able to import the fs module with the following line of code.

index.ts
import { promises as fsPromises } from 'fs';

# Add node to the types array in your tsconfig.json

If your error has not been resolved, open your tsconfig.json file and make sure the types array contains the string node.

tsconfig.json
{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the fs module.

# Delete your node_modules directory and rerun npm install

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

Make sure to restart your IDE if the error persists. VSCode glitches often and a reboot solves things sometimes.

If the error persists, follow the instructions in my Cannot find module 'X' Error in TypeScript article.

# An example of reading a directory and a file using fs promises

Here is an example of how you would read the contents of a directory and the contents of a file named another-file.ts located in the same directory using fs promises.

index.ts
import { promises as fsPromises } from 'fs'; import * as path from 'path'; async function readFile() { try { // โœ… Read contents of directory const dirContents = await fsPromises.readdir(__dirname); console.log(dirContents); // โœ… Read contents of `another-file.ts` in the same directory const fileContents = await fsPromises.readFile( path.join(__dirname, './another-file.ts'), { encoding: 'utf-8' }, ); console.log(fileContents); } catch (err) { console.log('error is: ', err); } } readFile();

You could rename the import to something else if fsPromises seems too long, e.g. fsp, etc.

Here is the output in my terminal, showing the files in the directory and the contents of another-file.ts.

fs promises read file success

# Cannot find module 'path' Error in TypeScript

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for Node by running the command npm i -D @types/node.

You can then import path with the following line of code import * as path from 'path'.

cannot find module path

# Install the typings for Node.js

Make sure to install the typings for Node by opening your terminal in your project's root directory and then run the following command:

shell
# with NPM npm i -D @types/node # with YARN yarn add @types/node --dev

Now you can import the path module with the following line of code.

index.ts
import * as path from 'path'; console.log(path);

# Add node to the types array in your tsconfig.json file

If your error has not been resolved, open your tsconfig.json file and make sure the types array contains the string node.

tsconfig.json
{ "compilerOptions": { "types": [ "node" ] }, }
This should fix the error and now TypeScript should be able to find the type definitions for the path module.

# Delete your node_modules and reinstall your dependencies

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

If you are on macOS or Linux, issue the following commands in bash or zsh.

shell
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

If you are on Windows, issue the following commands in CMD.

cmd
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐Ÿ‘‡๏ธ clean npm cache npm cache clean --force # ๐Ÿ‘‡๏ธ install packages npm install

Make sure to restart your IDE if the error persists. VSCode glitches often and a reboot solves things sometimes.

If the error persists, follow the instructions in my Cannot find module 'X' error in TypeScript article.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev