Skip to content

[Bug] Not working with ESM modules #134

@bhvngt

Description

@bhvngt

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

Activity

nonara

nonara commented on Aug 21, 2021

@nonara
Collaborator

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.

added a commit that references this issue on Aug 21, 2021
8185831
bhvngt

bhvngt commented on Aug 22, 2021

@bhvngt
Author

Thanks @nonara. That was really quick. Appreciate your help.

bhvngt

bhvngt commented on Nov 10, 2021

@bhvngt
Author

Since TypeStrong/ts-node#1439 has been merged, is there any other blocker to this issue?

Appreciate your effort and contribution @nonara.

nonara

nonara commented on Nov 14, 2021

@nonara
Collaborator

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

nonara commented on Nov 15, 2021

@nonara
Collaborator

Feature is finished and working! You can have a look here:

Shouldn't be too long before we have a release ready.

bhvngt

bhvngt commented on Nov 15, 2021

@bhvngt
Author

Thanks @nonara. Will check this out.

xpuu

xpuu commented on Jan 30, 2022

@xpuu

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

bhvngt commented on Jan 31, 2022

@bhvngt
Author

@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

nonara commented on Jan 31, 2022

@nonara
Collaborator

@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

xpuu commented on Jan 31, 2022

@xpuu

@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!

changed the title [-]Not working with ESM modules[/-] [+][Bug] Not working with ESM modules[/+] on Oct 20, 2022
halfbakedsneed

halfbakedsneed commented on Jun 19, 2023

@halfbakedsneed

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.

added
BugSomething isn't working
and removed
BugSomething isn't working
on Jun 19, 2023
danielpza

danielpza commented on Jun 19, 2023

@danielpza
Member

Hey everyone, until v4 is ready, if you make a PR we can review it and try to merge it to v3

nonara

nonara commented on Jun 20, 2023

@nonara
Collaborator

@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.

gralony

gralony commented on Apr 10, 2024

@gralony

Hey any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nonara@xpuu@halfbakedsneed@danielpza@bhvngt

        Issue actions

          [Bug] Not working with ESM modules · Issue #134 · LeDDGroup/typescript-transform-paths