Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: The requested module 'node:fs/promises' does not provide an export named 'constants' #736

Closed
1 task
MistakingManx opened this issue Dec 22, 2022 · 7 comments · Fixed by #740
Closed
1 task
Labels
bug Priority: 1

Comments

@MistakingManx
Copy link

MistakingManx commented Dec 22, 2022

Issue Type

  • Bug?

Current/Missing Behaviour

import { access, constants, readFile, rm } from "node:fs/promises";
                 ^^^^^^^^^
SyntaxError: The requested module 'node:fs/promises' does not provide an export named 'constants'

This error is displayed, I'd use the cli version, but my NPM doesn't make commands globally usable.

Expected/Proposed Behaviour

it to work

Additional Info

I don't usually work with ESM, I tried using Common JS, but it was just erroring with 1 so I couldn't figure out what was wrong.
I tried to make an ESM script, but this was the error, and using import() from Common JS yielded the same error.

  • Operating System: Windows
  • Node version: v17.0.1
  • NW.js version: 0.68.0
@MistakingManx
Copy link
Author

I tested a little tiny bit and importing "fs" gave me the constants, but "node:fs/promises" did not.
is this a Node.JS version problem then?

@MistakingManx
Copy link
Author

Switching to the version stated 14.19 gave another error about cp not being apart of fs/promises.
What version should I use?

@ayushmanchhabra
Copy link
Collaborator

Hello! The current stable version for nw-builder is v3.8.6. I'll put this in the docs so its known. The syntax error has been fixed in git. I'm working on #729 after which I'll probably make a minor release. I'd suggest using the stable version for now though, apologies for the confusion!

@MistakingManx
Copy link
Author

I ended up switching between versions until one of them worked, I think it was Node v19, but I mix numbers up really easy.

@aztack
Copy link

aztack commented May 29, 2023

4.2.3 still have this syntax error:
image

@ayushmanchhabra
Copy link
Collaborator

ayushmanchhabra commented May 29, 2023

I realise what the problem is now:

  1. nw-builder assumes you should have the latest Node.js installed which should not be the case. Maintaince, current and latest versions should all be supported.
  2. The way command line args are parsed, invalid args are ignored (read: people using v3 syntax for v4). In this case the default args are used. Combine that with [1] and we have a weird developer experience.

@TheJaredWilcurt
Copy link
Member

TheJaredWilcurt commented May 29, 2023

"Maintenance, current, and latest should all be supported" - A9U3HC

Each library maintainer can pick and choose what their library does and doesn't support. You can use a Node Version Manager to switch to different versions. You'll find the least trouble with NW.js (and really all technology related to JavaScript) by sticking with the latest versions of everything. But specifically making sure your global Node.js version matches the version built in to NW.js.

Node Version Managers:

OS Nodist nvm-windows nvm Volta
Windows 7 X X
Windows 8 X
Windows 10 X X
Windows 11 X X
Linux X X
OSX X X

The last NW.js version with Node 14 support was 0.49.1 (from October 2020). You would be best off switching to the latest version of NW.js (0.76.1) and installing the same version of Node.js globally (20.1.0).

There are some legitimate reasons to use older versions of NW.js. And maybe you have one. Others might too. Ideally nw-builder would work with all versions of NW.js and Node. But for such a complex task as automated building of a desktop app, that is a very tall order. Unfortunately there are many parts of the Node.js API that only run in certain ranges of Node versions. So there can be no one singular codebase that works perfectly in all versions, and even if we attempted, it would mean giving up any use of dependencies to solve common problems (like downloading and unzipping a file, etc).

If there is a way to transpile newer Node to older versions we could try that, but the only option I know of is Babel, and it doesn't work for the supplied code.

The node: prefix was not added until Node 18, but the transpiler does not convert it to older approach, so the code it generates would just throw this error:

Uncaught Error: Cannot find module 'node:fs/promises'

Recommendations

Option 1: Stick to the latest

  1. Uninstall Node.js, reboot, delete all remaining Node references (appData, program files, etc)
  2. Install one of the Node version managers linked above
  3. Use it to install Node.js v20.1.0
  4. Update your project to use NW.js v0.76.1
  5. Routinely update your dependencies and when you need to update NW.js, also update to the same Node.js version

Option 2: Use older automated build tools

You can try an older version of nw-builder or the now-defunct nwjs-builder-phoenix. These options may work if your codebase is locked in to using an older version of NW.js. Once you can update to the latest NW.js version, then you can also update to the latest nw-builder.

Option 3: Write your own custom build script

Depending on your project's needs you may be able write a build script that npm install's using an older version of Node that matches your older NW.js version. Then store that node_modules folder in a safe place. Then it switches Node versions to use a newer option and then uses nw-builder to do all the heavy lifting, then at the end swap out the node_modules folder in the built version with your original copy generated with your older node version.

Or you may need to abandon use of nw-builder with your project and write your own custom build script entirely from scratch. This has been the approach most of the community has taken for years. However, if whatever is preventing you from updating to the latest NW.js version ever changes you can then switch back and re-adopt nw-builder.


In all of these scenarios we are assuming your global Node version matches your NW.js's Node version. As this should always be the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Priority: 1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants