Linker: resolve to modules.exports
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 21
- Forks
- 23
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 17
Description
I think I've hit an issue deep in our module resolution in the runtime's linker (module loader).
Repro Steps
Basically the sort-term symptom is: we can't do import anyAscii from 'any-ascii';. When trying to run the build adaptor through the CLI (and ditto the worker), you get this error:
/Users/openfn/Workspace/adaptors/packages/salesforce/dist/index.cjs:207
var import_any_ascii = __toESM(require("any-ascii"), 1);
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/openfn/Workspace/adaptors/node_modules/.pnpm/any-ascii@0.3.2/node_modules/any-ascii/any-ascii.js from /Users/openfn/Workspace/adaptors/packages/salesforce/dist/index.cjs not supported.
Instead change the require of any-ascii.js in /Users/openfn/Workspace/adaptors/packages/salesforce/dist/index.cjs to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/Users/openfn/Workspace/adaptors/packages/salesforce/dist/index.cjs:207:32)
at async Promise.all (index 0)
at async linker (file:///Users/openfn/.asdf/installs/nodejs/18.12.1/lib/node_modules/@openfn/cli/node_modules/@openfn/runtime/dist/index.js:202:19)
at async file:///Users/openfn/.asdf/installs/nodejs/18.12.1/lib/node_modules/@openfn/cli/node_modules/@openfn/runtime/dist/index.js:279:22
at async Promise.all (index 0)
at async module_loader_default (file:///Users/openfn/.asdf/installs/nodejs/18.12.1/lib/node_modules/@openfn/cli/node_modules/@openfn/runtime/dist/index.js:277:3)
at async prepareJob (file:///Users/openfn/.asdf/installs/nodejs/18.12.1/lib/node_modules/@openfn/cli/node_modules/@openfn/runtime/dist/index.js:397:21)
at async file:///Users/openfn/.asdf/installs/nodejs/18.12.1/lib/node_modules/@openfn/cli/node_modules/@openfn/runtime/dist/index.js:362:35 {
code: 'ERR_REQUIRE_ESM'
}
Node.js v18.12.1
What's Occurring
Tricky one to explain this.
In the runtime, the linker module will take a specifier like any-ascii and try to resolve it to a path in the repo. It does this by looking in the repo, finding the right module folder, reading the package.json, and looking for its entry point, as declared in pkg.main.
any-ascii is a pure ESM module which happens to use the exports property of package.json.
I think that because we don't parse exports, we don't resolve the module path properly, and so the module fails to load.
And another thing
It looks like we're actually loading the CJS adaptor code here. Shouldn't we be loading the ESM format?
I have a feeling we're doing this to maintain parity with the old runtime. Or maybe old adaptors? But that doesn't seem necessary right now.
Alternatively we're not actually in control of this and for some reason node is choosing to use the CJS format.
It's probably something to do with adaptors - maybe we're using CJS because that's all the pre-monorepo adaptors support
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the runtime linker entry point shown in @openfn/runtime/dist/index.js and reproduce the failure by running the build adaptor through the CLI or worker with import anyAscii from 'any-ascii'. Inspect how package.json main and exports are resolved and which ESM or CJS entry is selected; done means the import loads successfully in the affected runtime paths.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- backend, cli, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100