-
-
Notifications
You must be signed in to change notification settings - Fork 28
Open
Labels
Description
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/ts-node@10.2.1_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/ts-node@10.2.1_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:774:9)
at moduleResolve (/projects/common/temp/node_modules/.pnpm/ts-node@10.2.1_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:815:18)
at Object.defaultResolve (/projects/common/temp/node_modules/.pnpm/ts-node@10.2.1_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist-raw/node-esm-resolve-implementation.js:929:11)
at /projects/common/temp/node_modules/.pnpm/ts-node@10.2.1_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/src/esm.ts:68:38
at Generator.next (<anonymous>)
at /projects/common/temp/node_modules/.pnpm/ts-node@10.2.1_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist/esm.js:8:71
at new Promise (<anonymous>)
at __awaiter (/projects/common/temp/node_modules/.pnpm/ts-node@10.2.1_c780171742f6906a053a603dfa210a4e/node_modules/ts-node/dist/esm.js:4:12)
at resolve (/projects/common/temp/node_modules/.pnpm/ts-node@10.2.1_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
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
nonara commentedon Aug 21, 2021
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.feat: Add esm support for ts-node register (closes #134)
bhvngt commentedon Aug 22, 2021
Thanks @nonara. That was really quick. Appreciate your help.
bhvngt commentedon Nov 10, 2021
Since TypeStrong/ts-node#1439 has been merged, is there any other blocker to this issue?
Appreciate your effort and contribution @nonara.
nonara commentedon Nov 14, 2021
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!
nonara commentedon Nov 15, 2021
Feature is finished and working! You can have a look here:
Shouldn't be too long before we have a release ready.
bhvngt commentedon Nov 15, 2021
Thanks @nonara. Will check this out.
xpuu commentedon Jan 30, 2022
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?
bhvngt commentedon Jan 31, 2022
@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.
nonara commentedon Jan 31, 2022
@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.
xpuu commentedon Jan 31, 2022
@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!
[-]Not working with ESM modules[/-][+][Bug] Not working with ESM modules[/+]halfbakedsneed commentedon Jun 19, 2023
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.
danielpza commentedon Jun 19, 2023
Hey everyone, until v4 is ready, if you make a PR we can review it and try to merge it to v3
nonara commentedon Jun 20, 2023
@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
andts-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.
esModuleInterop
? #186gralony commentedon Apr 10, 2024
Hey any update on this?