Skip to content

Conversation

@imakira
Copy link

@imakira imakira commented Jan 8, 2026

Best effort Module Resolution Algorithm implementation based on Node.js 25.2.1 as documented in https://nodejs.org/api/esm.html#resolution-and-loading-algorithm.

The following features are not implemented:

  • DETECT_MODULE_SYNTAX(source) is not implemented
  • In ESM_FILE_FORMAT, node.js will try to guess the format of a file with no extension. I kept the original behavoir of esmFileFormat which is throwing an error (Otherwise it will break some tests on some other places where I dont want to touch...)
  • ".wasm" files are not handled by ESM_FILE_FORMAT

This patch breaks one unit test:

@Test
public void dontImportCommonJs() throws IOException {
	final String src = "import('with-package').then(x => {throw 'unexpected'}).catch(console.log);";
	final String out = "TypeError: Unsupported file extension: '" + getTestRootFolderUrl() + "node_modules/with-package/alternative-index.js'\n";
	runAndExpectOutput(src, out);
}

The previous behavoir with regard to using import with CommonJS modules was:

  • import() can't import a commonjs module with .js extension, in a package not marked with "type"="module"
  • import() can import a commonjs module with .cjs extension.
  • import() can import a commonjs module with .js extension but not in a package

As import/import() can be used to load JavaScript CommonJS modules stated in packages.html.
I believe it is fair to expect import/import() to be able to import CommonJS modules.
In this patch, import can import a commonjs module with a cjs extension, or with a js extension and in a package not marked with "type"="module".

I couldn't get WebAssemblySimpleTestSuite working correctly (error: WebAssembly API enabled but "wasm" language cannot be accessed! Make sure the "wasm" l
anguage is found on the module path and one of the permitted languages when creating the Context). All other tests are running correctly.

Another thing to note:

In this patch, if esmResolve returns a result and esmFileFormat returns CommonJs, it will pass the resoluted path directly to tryLoadingAsCommonjsModule. Instead of the original specifier.
I don't know if this is the best way to do it, but at least we can import a CommonJs module exported in exports using import now (ideally we should make require also support it).

This patch is based on #904

sgammon and others added 2 commits January 9, 2026 00:11
If a module `package.json` specifies `exports`, GraalJs will now read
them and prefer exports over standard resolution; export types can be
registered by the developer as preferred. In lieu of these types (and
as a default), the following export types are preferred, in order:

- `graaljs`
- `import` (in ESM)
- `require`
- `default`

Fixes and closes oracle#903

Relates-to: oracle#903
Signed-off-by: Sam Gammon <sam@elide.dev>
@oracle-contributor-agreement
Copy link

Thank you for your pull request and welcome to our community! To contribute, please sign the Oracle Contributor Agreement (OCA).
The following contributors of this PR have not signed the OCA:

To sign the OCA, please create an Oracle account and sign the OCA in Oracle's Contributor Agreement Application.

When signing the OCA, please provide your GitHub username. After signing the OCA and getting an OCA approval from Oracle, this PR will be automatically updated.

If you are an Oracle employee, please make sure that you are a member of the main Oracle GitHub organization, and your membership in this organization is public.

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Required At least one contributor does not have an approved Oracle Contributor Agreement. label Jan 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Required At least one contributor does not have an approved Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants