-
Hi there, I'm using Yarn yarn node --loader ts-node/esm scripts/build.ts Although It seems that what's specified via the Any workaround? Should I open an issue? Note: everything works well if I unplug |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 19 replies
-
This is a current limitation of the Node loader API, Yarn itself cannot address that. You can follow the discussion on the Node repo: |
Beta Was this translation helpful? Give feedback.
-
@tavoyne you can use $ babel-node -x .ts ./scripts/build.ts You can find a complete example in Relay Starter Kit → |
Beta Was this translation helpful? Give feedback.
-
loader chaining was merged recently - see nodejs/node#42623 |
Beta Was this translation helpful? Give feedback.
-
https://github.com/nodejs/node/releases/tag/v16.17.0 chaining loader are now supporting by 16.17.0 |
Beta Was this translation helpful? Give feedback.
-
Bummer that we cannot use Yarn PNP with ts-node, or do much to change things until other projects get some things landed. Is there any workaround currently for running Typescript command-line scripts while using Yarn PNP? |
Beta Was this translation helpful? Give feedback.
-
ts-import should be a workaround (the entrypoint file in script needs to be converted to |
Beta Was this translation helpful? Give feedback.
-
Since #43772 was merged in node 19.6.0 it seems to be possible to run esm typescript with ts-node and yarn pnp:
But to import other ts/tsx files by a full path with ".js" extension (as esm requires) had to write a small custom loader: Custom loader code// .pnp-ts.loader.mjs
import Path from 'node:path';
import URL from 'node:url';
import FS from 'node:fs';
const mapping = new Map([
['.js', ['.js', '.ts', '.tsx', '.jsx']],
['.cjs', ['.cjs', '.cts']],
['.mjs', ['.mjs', '.mts']],
['.jsx', ['.jsx', '.tsx']],
]);
export function resolve(specifier, context, next) {
if (!specifier.startsWith('.')) {
return next(specifier, context);
}
const parentURL = context.parentURL;
if (!parentURL || !parentURL.startsWith('file:') || parentURL.includes('/.yarn/')) {
return next(specifier, context);
}
const specifiedExtension = Path.extname(specifier);
const sourceExtensions = mapping.get(specifiedExtension);
if (!sourceExtensions) {
return next(specifier, context);
}
const location = Path.dirname(URL.fileURLToPath(parentURL));
const required = specifier.slice(0, -specifiedExtension.length);
const path = Path.join(location, required);
for (const sourceExtension of sourceExtensions) {
if (FS.existsSync(path + sourceExtension)) {
return next(required + sourceExtension, context);
}
}
return next(specifier, context);
} Also, there is
|
Beta Was this translation helpful? Give feedback.
-
Confirmed that using a version of Node compiled with nodejs/node#43772 results in chain loading Yarn PNP and another loader (in our case, it's for hotpatching ESM modules with https://github.com/DataDog/import-in-the-middle) succeeding. |
Beta Was this translation helpful? Give feedback.
This is a current limitation of the Node loader API, Yarn itself cannot address that. You can follow the discussion on the Node repo: