How can I import byteArray with modern ES import?

(Repost, because the sub-category didn’t get set first time, and the post seems to have gone unnoticed).

I can’t import byteArray using the new ES module style of import. I’m using gjs 1.68.1. ‘gi://’ imports and relative imports work, but not byteArray. The docs give this example of importing a built-in module:

import system from 'system';

(although it says elsewhere that a .js extension is mandatory, but maybe that only applies to relative imports?).

So I tried:

import byteArray from 'byteArray';

which resulted in:

JS ERROR: ImportError: Unknown module: 'byteArray'

I’ve tried adding the extension, changing it to all lower case, capitalizing the B, adding a leading slash etc, but I haven’t found anything that works apart from resorting to the legacy imports global object.

Is byteArray not supported by ES module import (yet)?

1 Like

You don’t need to repost: you can edit the category and tags.

This is a good question. We don’t have a byteArray module because it is for the most part already replaced by Uint8Array. For converting text from byte arrays to strings and vice versa, you still need it, so in that case you can still use imports.byteArray for the time being. In GNOME 41 we are planning to include implementations of TextEncoder and TextDecoder so at that point you can delete the remaining uses of imports.byteArray.

The extension is mandatory for file imports. The names of built-in imports are just words, not corresponding to files :grinning_face_with_smiling_eyes:

Yes, I only want it for the string conversion functions. Having TextEncoder and TextDecoder will be good, and I’m pleased to see gjs progress towards better compatibility with the browser and node. In the meantime I could easily (if ignoring non-UTF8) add a polyfill.

AFAICT you should also omit the extension if you’re importing a node library. Not applicable for gjs of course, but gi imports in gjs also have no extension.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.