caoccao/Javet

ESM Import Default does not work

Closed this issue ยท 8 comments

Hi! First of all, thank you so much for your effort on this project.

For such a code example like this:
import buffer from 'node:buffer';

I get this kind of error:
com.caoccao.javet.exceptions.JavetExecutionException: SyntaxError: The requested module 'node:buffer' does not provide an export named 'default'

as far as I read from docs, javet is supposed to support it.

this kind of codes are supported from commonjs:
const fs = require('fs'); but why not like import fs from "fs"

Thank you for your attention.

Thank you. Yes, it's supported. Please refer to this doc for details.

I believe there is supposed to be a solution for this rather than a hacky module since nodejs is directly able to run the same code without an issue. Because I'm trying to run a pre-built node app. Such as the output of the SvelteKit application. It provides a simple build folder and under it pre-compiled js files. Wouldn't like to touch pre-compiled files ๐Ÿ˜…

That's not a hacky solution, but the current best way of exposing the Node.js built-in modules as ESM. It's just one line of code dealing with Javet. There are no code changes required to your JS files.

Well, then v8Runtime.setV8ModuleResolver(new JavetBuiltInModuleResolver()); this code does not work for this line in JavaScript script file:

import buffer from 'node:buffer';

it gives the error I provided above:
com.caoccao.javet.exceptions.JavetExecutionException: SyntaxError: The requested module 'node:buffer' does not provide an export named 'default'

this was the issue already in the beginning I had. ๐Ÿ˜…

if I change the line in that pre-compiled JavaScript file like this: import * as buffer from 'node:buffer' it works but this is what I'm trying to avoid.

I think that is a bug of JavetBuiltInModuleResolver. I'll fix it later. In the meanwhile, you may create your own resolver to expose default to make it work so that you don't need to wait for the patch.

I wasn't aware of such bug until you reported it. Thank you.

Thank you for your extra attention.

It's fixed. Please wait for the next release. And you may borrow that few lines of change to your own resolver.

This was fast! And I tried now it works! Thanks again.