39

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

csharpbd
  • 3,786
  • 4
  • 23
  • 32
stillborn1
  • 507
  • 1
  • 4
  • 10

11 Answers11

38

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.
  • 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://stackoverflow.com/questions/8191459/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. – Peter Palmer Nov 21 '21 at 15:56
31

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

  • 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. – Boat Sep 20 '21 at 09:50
  • 5
    Upgrading to node v14 worked for me. Thanks! – Meekohi Oct 08 '21 at 21:00
  • 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. – Marcosaurios Nov 09 '22 at 12:56
  • Switching from `"^22.10.5"` to `"22.10.5"` worked for me – Shane Gannon Feb 02 '23 at 11:03
13

In that case I fixed the problem in that way:

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

Instead of:

const fs = require('fs/promises');
svarog
  • 9,477
  • 4
  • 61
  • 77
gBall
  • 131
  • 1
  • 2
  • 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'` – itpropro Feb 20 '23 at 17:38
12

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

Hieu Nguyen
  • 121
  • 2
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.

Cels
  • 1,212
  • 18
  • 25
4

Upgrade to electron-updater@5.0.0. It has patch changes replacing fs/promises with fs-extra to support legacy versions of electron.

Erich
  • 2,408
  • 18
  • 40
B4Le
  • 77
  • 2
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

KiwenLau
  • 2,502
  • 1
  • 19
  • 23
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.

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..

Muhammad Numan
  • 237
  • 4
  • 20
0

2023 / ESM answer

npm i @types/node

Then in your JS/TS:

import { promises } from "node:fs";

or to be explicit (since promises is a bit vague)

import { promises as fsPromises } from "node:fs";
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
-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 electron-builder@23.0.2

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)
Enzo Degraeve
  • 165
  • 1
  • 1
  • 13
Paul
  • 223
  • 2
  • 14