Skip to content
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

[Bug] Not working with ESM modules #134

Open
bhvngt opened this issue Aug 21, 2021 · 14 comments
Open

[Bug] Not working with ESM modules #134

bhvngt opened this issue Aug 21, 2021 · 14 comments
Labels
Bug Something isn't working Feature Request Fix Ready Good First Issue Good for newcomers

Comments

@bhvngt
Copy link

bhvngt commented Aug 21, 2021

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

@nonara
Copy link
Collaborator

nonara commented 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.

@bhvngt
Copy link
Author

bhvngt commented Aug 22, 2021

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

@nonara nonara added the Awaiting Upstream Fix Bug-fix for dependency is in progress label Sep 12, 2021
@bhvngt
Copy link
Author

bhvngt commented 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
Copy link
Collaborator

nonara commented 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 nonara added Fix Ready Feature Request and removed Awaiting Upstream Fix Bug-fix for dependency is in progress labels Nov 15, 2021
@nonara
Copy link
Collaborator

nonara commented 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
Copy link
Author

bhvngt commented Nov 15, 2021

Thanks @nonara. Will check this out.

@xpuu
Copy link

xpuu commented 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
Copy link
Author

bhvngt commented 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
Copy link
Collaborator

nonara commented 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
Copy link

xpuu commented 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!

@nonara nonara changed the title Not working with ESM modules [Bug] Not working with ESM modules Oct 20, 2022
@halfbakedsneed
Copy link

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 danielpza added Bug Something isn't working and removed Bug Something isn't working labels Jun 19, 2023
@danielpza
Copy link
Member

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

@danielpza danielpza added Bug Something isn't working Good First Issue Good for newcomers labels Jun 19, 2023
@nonara
Copy link
Collaborator

nonara commented 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 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
Copy link

gralony commented Apr 10, 2024

Hey any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Feature Request Fix Ready Good First Issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

6 participants