Reputation: 3996
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?
Upvotes: 29
Views: 81629
Reputation: 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
Upvotes: -1
Reputation: 51
In my case VSCode added a arbitrary import from electron. After removing it my application worked.
import { Menu } from 'electron';
Upvotes: 0
Reputation: 57175
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.
Upvotes: 2
Reputation: 41
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
Upvotes: 4
Reputation: 1130
I was facing the same Error like TypeError: fs.existsSync is not a function
So, I figured out that one extra line was added automatically which was creating this issue in import.
after removing this line from import
import { TRUE } from "node-sass";
the issue has been resolved.
Upvotes: 28
Reputation: 435
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 fromnode-sass
in yournode_modules
file, and you can't edit that file. So look for and removeimport { types } from "node-sass"
Upvotes: 1
Reputation: 165
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.
Upvotes: 8
Reputation: 92460
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/
Upvotes: 4