graaljs icon indicating copy to clipboard operation
graaljs copied to clipboard

"import" fails if file extension is not included

Open zikifer opened this issue 1 year ago • 2 comments

When importing a local (non-NPM) module the import fails if the file extension is not included. For example import { Worker } from './worker';

If "js.commonjs-require" is "false" the error is Error: Error reading: /path/to/file/worker If "js.commonjs-require" is "true" the error is TypeError: Module not found: './worker'

Example attached. graaljs-import.zip

To run: mvn package exec:java

Expected: It should be able to resolve ./worker to ./worker.js, as node does.

zikifer avatar Mar 08 '23 16:03 zikifer

Hi, Thank you for reaching out, we'll take a look into it shortly.

oubidar-Abderrahim avatar Mar 13 '23 14:03 oubidar-Abderrahim

I had a quick look at your example, and -- if I am not mistaken -- node.js also throws an error with import './worker' in your case:

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/Users/danieleb/Downloads/graaljs-import/src/main/resources/worker' imported from /Users/danieleb/Downloads/graaljs-import/src/main/resources/main.mjs
Did you mean to import ../worker.js?

In any case, you can use a Truffle FS to customize how ES modules are loaded, and there is no need to use js.commonjs-require to load non-NPM modules like in your case. We have an example here, and in our unit tests.

eleinadani avatar Mar 14 '23 10:03 eleinadani