TypeError fs.existsSync不是函数

我想从节点导入“ fs”模块,但是遇到了麻烦。

如果我使用require,我会得到fs.existsSync不是函数错误。

如果我使用ES6导入,则会得到TypeError:Object(...)不是一个函数,因为Webpack显然将其编译为对象,例如:Object(fs__WEBPACK_IMPORTED_MODULE_4__["existsSync"])

我正在使用React

我的代码使用CommonJS导入:

const csv = require("csvtojson");
const path = require("path");
const fs = require("fs");
const R = require("ramda");

const csvPath = path.join(__dirname,`../../../public/languages/${process.env.REact_APP_VENDOR}`);
const jsonPath = path.join(__dirname,`../../../public/locales/`);

console.log(`csvPath:  ${csvPath}`);
console.log(`jsonPath: ${jsonPath}`);

if (!fs.existsSync(jsonPath)) {
  fs.mkdirSync(jsonPath);
}
...

我的代码使用ES6导入:

import csv from "csvtojson";
import { join } from "path";
import { existsSync,mkdirSync,readdir,writeFile } from "fs";
import { compose,pathOr,split,last,map } from "ramda";

const csvPath = join(__dirname,`../../../public/languages/${process.env.REact_APP_VENDOR}`);
const jsonPath = join(__dirname,`../../../public/locales/`);

console.log(`csvPath:  ${csvPath}`);
console.log(`jsonPath: ${jsonPath}`);

if (!existsSync(jsonPath)) {
  mkdirSync(jsonPath);
}
...

如何导入该模块以使用它? 我正在使用节点版本10.16.3

jackydr8410 回答:TypeError fs.existsSync不是函数

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3150228.html

大家都在问