Cannot find module 'fs/promises' Electron JS
Asked Answered
G

11

44

Good morning,

I have created a program in Vue JS, this connects with an API that I have created in a main.js file to execute system commands.

The problem I have is that when compiling for production with electron I get the following error:

ERROR

I use the command npm run electron: build

When I use npm run electron:serve work without problems

Anyone have any idea why is the error and how to fix it? Thanks

Genome answered 22/6, 2021 at 14:25 Comment(1)
I had the same issue .. Update to the newest nodejs-Version will fix this issue.Snub
I
42

I experienced this issue a few days ago as well. I realized that trying to fix another issue, I deleted the node_modules folder and the package-lock.json file, then run the npm install command. This made the build to fail with 'fs/promises'. There are 2 solutions to this issue:

  1. Download the latest stable Node version. This should have the 'fs/promises' module and will fix the issue.
  2. Delete the node_modules folder and bring back the old package-lock.json file to ensure that the package versions remain the same. Then run the npm install command and the issue should be fixed.
Inequality answered 25/6, 2021 at 10:38 Comment(1)
Hey there, I did upgrade my NodeJS version and worked for me, I used terminal commands in order to upgrade in a faster way, you can follow the instructions here: https://mcmap.net/q/45702/-how-do-i-update-node-js. Take into consideration that maybe other projects you have are incompatible with this new version you are installing, in that case, consider using NVM.Demean
U
33

downgrade electron "electron-builder": "^22.10.5", or upgrade nodejs to 14+ v

Unending answered 7/8, 2021 at 18:48 Comment(4)
Downgrading the electron-builder version worked for me, with node 12.18.4. This should be the accepted answer since retrieving old package-lock.json is not going to work when installing everything first time.Astraddle
Upgrading to node v14 worked for me. Thanks!Ironworker
If you're not using this dependency directly and this pops up at some building stage, it's very likely it's the Node version. Upgrading to 14 solved.Whoredom
Switching from "^22.10.5" to "22.10.5" worked for meHanselka
A
14

In that case I fixed the problem in that way:

const fs = require('fs').promises;

Instead of:

const fs = require('fs/promises');
Anthracosis answered 19/1, 2022 at 15:21 Comment(1)
You should not use old syntax in 2023. This is the modern esm style import: import { readFile } from 'fs/promises' or without a specific import: import { promises as fs } from 'fs'Outnumber
S
12

downgrade to "electron-builder": "~22.10.5" is working for me

Stubbs answered 20/8, 2021 at 11:30 Comment(0)
B
10

In my case I was using nvm to manage multiple node versions.

During the npm package installation, and throughout development, I used Node v14 but for some reason, my terminal was pointing to Node v12 when I tried bundling my program afterwards.

Switching it back to Node v14 using nvm use 14 solved my issue.

So make sure you're using the correct node version.

Belen answered 20/12, 2021 at 10:51 Comment(0)
H
5

Upgrade to [email protected]. It has patch changes replacing fs/promises with fs-extra to support legacy versions of electron.

Hospital answered 1/6, 2022 at 13:55 Comment(0)
B
2

2023 / ESM answer

npm i @types/node

Then in your JS/TS:

import { readFile } from "node:fs/promises";

or to import the entire library (renaming it, since the name promises is a bit vague)

import fsPromises from "node:fs/promises";
Bergquist answered 5/7, 2023 at 16:7 Comment(0)
U
0

got the same error "Cannot find module 'fs/promises'" while I don't use electron.

so the problem is not only related to electron

solved the problem just by upgrading nodejs from v13.9.0 to v14.19.3

Unaccountedfor answered 19/6, 2022 at 13:23 Comment(0)
P
0

If this happens to you (and I'm not using Electron either), and you have to stay on Node 12 like me (because the code you are maintaining is ancient), pray that you can get to one of the npm-shrinkwrap.json files you used that worked, then go through package.json, force every version to what was in the shrinkwrap file, rm -rf node_modules, and npm install.

Prader answered 2/9, 2022 at 0:14 Comment(0)
H
0

I experienced this issue a few days ago. I realized that trying to fix another issue, I deleted the node_modules folder and the package-lock.json file, then run the

npm install

This made the build to fail with 'fs/promises'. Delete the node_modules folder and bring back the old package-lock.json file to ensure that the package versions remain the same then run the npm command with force

npm install --force 

it work for me..

Huh answered 31/1, 2023 at 5:23 Comment(0)
F
-2

I had the same problem, after upgrading the electron-builder from v. 21.4.0 to 23.0.2, updated with the command:   sudo npm install -g [email protected]

I solved updating npm, and then node.js.

  1. Update npm:

    sudo npm install -g npm@latest  

  2. Install nodejs from https://nodejs.org

Now it works with :  

  • Electron-builder: 23.0.2 (command electron-builder --version)
  • Npm: 8.7.0 (command npm --version)
  • Nodejs: v16.15.0 (command node --version)
Fonda answered 27/4, 2022 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.