Last updated: Feb 27, 2024
Reading timeยท5 min
If you got the "Cannot find module 'path'" error, click on the third subheading.
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'
.
Make sure to install the typings for Node by opening your terminal in your project's root directory and running the following command.
# ๐๏ธ 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.
import * as fs from 'fs'; console.log(fs);
node
to the types
array in your tsconfig.json
fileIf your error has not been resolved, open your
tsconfig.json file and make sure the
types
array contains the string node
.
{ "compilerOptions": { "types": [ "node" ] }, }
fs
module.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
.
# 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.
# 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.
fs
module in a TypeScript projectHere is an example of how you would read a file in the same directory named
another-file.ts
with the fs
module using TypeScript.
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
.
I've also written a detailed guide on how to
import and use the fs
module 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
.
Make sure to install the typings for Node, by opening your terminal in your project's root directory and then run the following command:
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.
import { promises as fsPromises } from 'fs';
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
.
{ "compilerOptions": { "types": [ "node" ] }, }
fs
module.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
.
# 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.
# 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.
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.
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
.
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'
.
Make sure to install the typings for Node by opening your terminal in your project's root directory and then run the following command:
# 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.
import * as path from 'path'; console.log(path);
node
to the types
array in your tsconfig.json
fileIf your error has not been resolved, open your tsconfig.json
file and make
sure the types
array contains the string node
.
{ "compilerOptions": { "types": [ "node" ] }, }
path
module.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
.
# 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.
# 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.