typescript-transform-paths
typescript-transform-paths copied to clipboard
[Bug] Not working with ESM modules
I am trying this out with my esm module with following configuration
tsconfig.tsnode.json
{
"include": ["src/**/*", "tests/**/*"],
"exclude": ["node_modules/*", "coverage/*"],
"ts-node": {
"transpileOnly": true,
"require": ["typescript-transform-paths/register"]
},
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": ["dom", "esnext"],
"moduleResolution": "node",
"strict": true,
"forceConsistentCasingInFileNames": true,
"importsNotUsedAsValues": "error",
"incremental": true,
"types": ["node", "jest"],
"baseUrl": ".",
"paths": {
"$src/*": ["./src/*"]
},
"isolatedModules": false,
"plugins": [
{ "transform": "typescript-transform-paths" },
{ "transform": "typescript-transform-paths", "afterDeclarations": true }
]
}
}
package.json
{
...
"type": "module"
...
}
b.ts
export const b: string = "Hello World!"
a.ts
import {b} from "$src/b"
console.log(b);
TS_NODE_PROJECT=tsconfig.tsnode.json node --loader ts-node/esm --es-module-specifier-resolution=node src/a.ts throws
/projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:774
throw new ERR_MODULE_NOT_FOUND(packageName, fileURLToPath(base));
^
CustomError: Cannot find package '$src' imported from /projects/libs/domain/src/a.ts
at packageResolve (/projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:774:9)
at moduleResolve (/projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:815:18)
at Object.defaultResolve (/projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:929:11)
at /projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/src/esm.ts:68:38
at Generator.next (<anonymous>)
at /projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist/esm.js:8:71
at new Promise (<anonymous>)
at __awaiter (/projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist/esm.js:4:12)
at resolve (/projects/common/temp/node_modules/.pnpm/[email protected]_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist/esm.js:32:16)
at Loader.resolve (node:internal/modules/esm/loader:89:40)
if I change import in a.ts to relative path import {b} from "./b", it works with the same command.
When used with commonjs works by changing module to commonjs under tsconfig and removing "type": "module" under package.json with cli TS_NODE_PROJECT=tsconfig.tsnode.json ts-node src/a.ts
Thanks for the report! I had a look at the ts-node source, and I think I know what's going on. Will be looking into it.
Update: I have a fix ready, but it requires an upstream PR on ts-node. I've submitted one, so we'll wait and see what happens there.
Thanks @nonara. That was really quick. Appreciate your help.
Since TypeStrong/ts-node#1439 has been merged, is there any other blocker to this issue?
Appreciate your effort and contribution @nonara.
No blocker, per se, but things have been incredibly busy. This is rolled into the upcoming new major release, which has a lot of changes. I'm working on wrapping that up and hope to have it out soon!
Feature is finished and working! You can have a look here:
- #145
- https://github.com/LeDDGroup/typescript-transform-paths/pull/145/commits/b62dd49856e53351d70599b4e2e116e96fcd3176
Shouldn't be too long before we have a release ready.
Thanks @nonara. Will check this out.
Hi @nonara, thanks for this useful lib. I'm looking forward to v4 release. I tried to test it in the meantime with the v4 branch, but without much success. Probably just me being tired and stupid. @bhvngt Did that work for you?
@xpuu I have moved on to using other libraries. I am hoping that when typescript releases their implementation supporting self-referencing package, we may have a more standard compliant way of handling this use case - moving away from relative imports.
@xpuu Thanks for the kind words! v4 is close, but there are a few outstanding issues I need to work out. Mainly, I need to finish the tests.
As many can understand in these unusual times, circumstances took me away from being able to do open source work for awhile. I'm very much looking forward to wrapping v4 and hoping to soon, but I need to get things stable first.
I've been swamped on contract work, but I've just launched a firm, which will help me offload some of the work and eventually free me up to work on open source again.
I do hope to be able to get this wrapped on one of the upcoming weekends, however.
@nonara Just so you know, I'm covered for now (solved this using some weird custom loader). Your message about your current workload really resonates with me. Good luck!
Hey @nonara, is there anything we can do to help with the release of v4? Would love to see it released as this transformer is the only way I've managed to handle NX path mappings at runtime without using esbuild. No ESM support is the only drawback.
Hey everyone, until v4 is ready, if you make a PR we can review it and try to merge it to v3
@halfbakedsneed I appreciate the offer!
ESM and replicating node's require behaviour is a tricky problem, but this is definitely long overdue.
I finally got some time to set aside and revamp OSS. I just finished a rewrite of ts-patch and ts-expose-internals.
I have to do a few quick PR and issue closes for node-html-markdown, which should be quick, then I'll get this thing wrapped and pushed out.
If you do want to help, feel free to have a look at the open branch and let me know if you think there's an area you feel you can take on. If memory serves, it was largely a matter of finishing tests, but I'll have to check again.
Hey any update on this?