question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Not working with ESM modules

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:10

github_iconTop GitHub Comments

3reactions
nonaracommented, 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.

1reaction
xpuucommented, 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Bug]: Not work with esm package · Issue #12076 · facebook/jest
If you run node src/main.js it works normally. Environment. System: OS: Windows 10 10.0.19043 CPU: (24) x64 AMD Ryzen 9 5900X 12 ...
Read more >
Node.js Bug Or Am I Doing it Wrong? ES Modules [closed]
This is not a bug in nodejs (running ESM modules works just fine). It's a configuration error on your part in some way...
Read more >
ECMAScript Modules - Jest
Jest ships with experimental support for ECMAScript Modules (ESM). The implementation may have bugs and lack features. For the latest status ...
Read more >
node-fetch Error [ERR_REQUIRE_ESM]: require() of ES ...
The node-fetch error "[ERR_REQUIRE_ESM]: require() of ES Module not supported" occurs because the node-fetch package has been converted to be an ESM-only ...
Read more >
Troubleshooting | ts-node - TypeStrong · GitHub
This error is thrown by node when a module has an unrecognized file extension, or no extension at all, and is being executed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found