24

In NodeJS I have:

const fs = require('fs');
if (!fs.existsSync("some_path")) {
...
}

But I get the error:

TypeError: fs.existsSync is not a function

After doing some searching, I read that Webpack brings its own require which clobbers node.js's require, so when you require a node.js core module that webpack can't resolve to one of your files or dependencies, it throws.

(My stack trace includes __webpack_require__)

But how can I fix it?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Gambit2007
  • 3,260
  • 13
  • 46
  • 86

8 Answers8

27

I was facing the same Error like TypeError: fs.existsSync is not a function

enter image description here

So, I figured out that one extra line was added automatically which was creating this issue in import.

enter image description here

after removing this line from import

import { TRUE } from "node-sass";

the issue has been resolved.

Krunal Rajkotiya
  • 1,070
  • 9
  • 14
  • 3
    This helped me, thanks! My editor (Visual Studio Code) somehow magically inserted `import { render } from 'node-sass';` at the top of my file. -.- – Page not found Nov 20 '20 at 02:07
7

I had the same error that you have. Your vscode might have added a new module to your js file. Remove that module and your app should work just fine.

KDSG
  • 145
  • 1
  • 6
4

You can allow webpack to use the Node's require and include fs etc. by targeting node in the config:

module.exports = {
  entry: './src/main.js',
  target: 'node',
  output: {
    path: path.join(__dirname, 'build'),
    filename: 'backend.js'
  }
}

As described here: https://webpack.js.org/concepts/targets/ and https://webpack.js.org/configuration/target/

Mark
  • 90,562
  • 7
  • 108
  • 148
  • This is your webpack config file: https://webpack.js.org/configuration/ (add `webpack.config.js` in the root folder). – Mark Dec 18 '18 at 21:42
  • I'm using a template created using `create-react-app` (i think) so right now i have 3 `webpack.config.js` files: `...node_modules/react-scripts/config/webpack.config.dev.js` `...node_modules/react-scripts/config/webpack.config.prod.js` `...node_modules/raphael/webpack.config.dev.js` Do i need to use one of them or create a new one? – Gambit2007 Dec 18 '18 at 21:46
  • @Gambit2007 Not sure about configuring `react`, sorry. – Mark Dec 18 '18 at 21:49
4

I was working on an electron application, I wanted to send a message from node and get in on the react side, but I was having that same issue when requiring ipcRenderer from electron, I tried import { ipcRenderer } from 'electron'; and const { ipceRenderer } = require('electron') This leads to an error due to webpack transforming node's require to its own webpack_require. See more info here

What worked for me was to use

const {ipcRenderer} = window.require('electron'); on the react side/renderer side from electron

Jhon Arias
  • 41
  • 3
  • ['TypeError: fs.existsSync is not a function' ReactJS and Electron](https://stackoverflow.com/questions/59477396/typeerror-fs-existssync-is-not-a-function-reactjs-and-electron) is probably a better thread for Electron problems. – ggorlen Apr 21 '22 at 15:46
  • get error: `Server Error ReferenceError: window is not defined` when use `window.require` – qg_java_17137 Jul 04 '23 at 01:14
2

In my case, I forgot that I'd only imported the promises API, const fs = require("fs").promises, which doesn't have exist or existsSync functions in Node 17.4.0.

To use exist or existsSync, make sure you've imported fs using the sync API (const fs = require("fs")).

Note: I'm adding this answer as a possible solution for future visitors to a canonical thread for the error, not OP who appears to have required fs correctly.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
1

It is nothing to worry about, check your code for something like import { types } from "node-sass";, it would have mistakenly and automatically imported without you know. Remove that line, and everything should work perfectly.

Even if it is not type, it is something from node-sass in your node_modules file, and you can't edit that file. So look for and remove import { types } from "node-sass"

Godstime
  • 365
  • 7
  • 14
0

In my case VSCode added a arbitrary import from electron. After removing it my application worked.

import { Menu } from 'electron';
David
  • 51
  • 1
  • 6
-1

In my case, i needed to send a message from the node to react. I tried importing ipcRenderer from 'electron'; and const ipceRenderer = require('electron') This results in an error owing to webpack changing the node's require to its own webpack require. See more info here

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 13 '22 at 16:26