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

typescript with ts-node and ESM support #47

Open
juergba opened this issue Jun 16, 2021 · 6 comments
Open

typescript with ts-node and ESM support #47

juergba opened this issue Jun 16, 2021 · 6 comments

Comments

@juergba
Copy link
Member

juergba commented Jun 16, 2021

Add a typescript sample using ts-node and node's native ESM support.
Note: ts-node uses Node's loader-hooks which are still experimental.

You can find info: ts-node/1007
and a working sample: ts-node/discussion

@cspotcode
Copy link

Thanks. Time permitting I'll send a pull request with a sample. Till then, I'm subscribed to this thread and can answer questions.

@Dunky13
Copy link

Dunky13 commented Sep 29, 2021

I'm looking for a solution on this as well

@jordaaash
Copy link

FWIW, here's a configuration for Mocha, TypeScript, and ESM that finally worked for me:
https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d

"node-option": ["experimental-specifier-resolution=node", "loader=ts-node/esm"] was the clincher.

@henryruhs
Copy link

henryruhs commented Oct 26, 2021

@jordansexton Thank you so much, that worked for me!

package.json

{
	"type": "module"
}

tsconfig.json

{
	"compilerOptions": {
		"module": "esnext",
		"moduleResolution": "node",
	}
}

.mocharc.json

{
	"node-option": [
		"experimental-specifier-resolution=node",
		"loader=ts-node/esm"
	]
}

@fcamblor
Copy link

fcamblor commented Jan 11, 2022

Thanks to all of you here, it helped me a lot :-)

I would simply add that one requirement is to use mocha@9 (mocha@9.1.3 for me) as I was on mocha@8 and I was getting following error :

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts"

ledyba added a commit to fairy-rockets/the-gear-of-seasons that referenced this issue May 1, 2022
falsefalse added a commit to falsefalse/yaf-extension that referenced this issue Apr 9, 2023
mocha + sinon + chai.

ESM + typescript is hard :-/
Editor TS, build TS and spec TS don't want to agree on module imports.
On top of that — Jakefile can not be ESM yet.
So src/ and /spec are modules, and project is not.
--watch won't work unless ran with --parallel

mochajs/mocha#4374
mochajs/mocha-examples#47
https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d
falsefalse added a commit to falsefalse/yaf-extension that referenced this issue Apr 9, 2023
mocha + sinon + chai.

ESM + typescript is hard :-/
Editor TS, build TS and spec TS don't want to agree on module imports.
On top of that — Jakefile can not be ESM yet.
So src/ and /spec are modules, and project is not.
--watch won't work unless ran with --parallel

mochajs/mocha#4374
mochajs/mocha-examples#47
https://gist.github.com/jordansexton/2a0c3c360aa700cc9528e89620e82c3d
@FauxFaux
Copy link

FauxFaux commented Mar 5, 2024

Hi everyone else from search results.

This only works on node 16, haven't been able to get it to work on 18 or 20.

Newer nodes don't like, and don't need(?) the "experimental-specifier-resolution=node",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants