-
-
Notifications
You must be signed in to change notification settings - Fork 536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent behaviour with moduleResolution: "node16"
and swc: true
for CommonJS module output
#2035
Comments
Looking at the swc side of things, it seems like the "import() gets transpiled to require()" issue can be fixed by setting But unfortunately due to #1856 it looks like that doesn't fix the issue when running the code via Back here, looking at the internals of the swc transpiler, it seems like this case is accounted for: Lines 1469 to 1474 in 47d4f45
But so far it's not obvious to me how to force ts-node/src/transpilers/swc.ts Line 211 in 47d4f45
(i.e. a way to force |
Final findings for today, which may indicate a fix for my project (and highlight my configuration mistakes):
My confusion was assuming that I'm going to go back and update my non-toy project with these changes to see if that fixes things, but I assume it will. Assuming my understanding of the above is correct, this issue can probably be closed, but if possible it would be great if the docs could point to this behaviour somewhere, if others are using the stale Happy to create a new issue for the doc updates if that's preferred! |
I decided to file this issue here after looking at both the
swc
andts-node
issues - apologies if this is a better fit forswc
! I'm also fully expecting to be told that there is additional swc configuration required, in which case this issue could be reframed as "add additional info on configuring swc to https://typestrong.org/ts-node/docs/swc/".Search Terms
moduleResolution
,swc
, CommonJS,ERR_REQUIRE_ESM
,import()
Expected Behavior
In a TypeScript project outputting CommonJS via
"module": "CommonJS"
,ts-node
should compile and run files the same regardless of theswc
option. See below for an example where this surprisingly wasn't the case.This came up when importing an ESM-only package into a TypeScript file targeting CommonJS. Node gives us the suggestion to
Instead change the require of index.js in /Users/jbateson/code/ts-node-swc-module-resolution-repro/index.ts to a dynamic import() which is available in all CommonJS modules.
, but this doesn't seem to work withswc: true
.Actual Behavior
Given the following
tsconfig.json
:the following code combining static ESM imports + dynamic imports of ESM-only code works fine:
but when adding
it outputs the following when invoked as
ts-node index.ts
:which implies that
swc
is transpiling theimport()
differently from the defaultts-node
/tsc
behaviour.I've "verified" this (kinda) by looking at the swc repl, which turns
import()
intorequire()
unconditionally when the target is CommonJS.Steps to reproduce the problem
See above, also see a link to a runnable version of the same minimal repro below.
Minimal reproduction
Clone this repo, run
yarn
to install dependencies, and then runts-node index.ts
to reproduce the above: https://github.com/jdb8/ts-node-swc-module-resolution-reproCommenting out
swc: true
will show the expected behaviour.Specifications
The text was updated successfully, but these errors were encountered: