I wrote this code and it works fine, that is written in TypeScript. When I use the same code in the test file in Cypress I get error TypeError: fs.readdir is not a function
import * as fs from 'fs'
let inputPath: String = "C:\\Users\\rkon";
let replacementString = "/";
let newInputPath = inputPath.split('\\').join(replacementString)
console.log('path after replacement: ' + newInputPath);
fs.readdir(newInputPath as string, function (err: any, files: any[]) {
//handling error
if (err) {
return console.log('Unable to scan directory: ' + err);
}
//listing all files using forEach
files.forEach(function (file) {
console.log('f: ' + file);
});
});
I verified the above code by first doing
>tsc temp.ts
>node temp.js
As I said it worked fine but not sure why does the same code does not work in Cypress and gives the error
TypeError: fs.readdir is not a function
Here is the tsconfig.json
{
"compilerOptions": {
"esModuleInterop": true,
"moduleResolution": "node",
"module": "commonjs",
"resolveJsonModule": true,
"skipLibCheck": true,
"noImplicitAny": true,
"sourceMap": true,
"allowJs": true,
"strict": true,
"target": "es2017",
"lib": ["es2017"],
"typeRoots": ["./typings","./node_modules/@types","node_modules/@types/node","node_modules/@types"],
"baseUrl": "../node_modules",
"paths": {
"*": ["./node_modules/@types/*"]
},
"outDir": "build/"
}
}
Here are the versions
>node -v
v12.13.0
>npm -v
6.12.0
>tsc -v
Version 3.7.2
>.\node_modules\.bin\cypress -v
Cypress package version: 3.5.0
Cypress binary version: 3.5.0
>ver
Microsoft Windows [Version 10.0.17763.805]
"typeRoots": ["./node_modules/@types"]
in package.json>tsc temp.ts --noResolve
then I get errortemp.ts:14:21 - error TS2307: Cannot find module 'fs'.