0

I want to import "fs" module from node but I'm having trouble.

If I use require I get fs.existsSync is not a function error.

If I use ES6 imports I get TypeError: Object(...) is not a function, because Webpack apparently compiles it to an object, like this: Object(fs__WEBPACK_IMPORTED_MODULE_4__["existsSync"])

I'm using React.

My code using CommonJS imports:

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);
}
...

my code using ES6 imports:

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);
}
...

How should I import the module to use it? I am using node version 10.16.3

Filip
  • 855
  • 2
  • 18
  • 38

0 Answers0