Replies: 5 comments 3 replies
-
Might be a mistake in the way you're passing --loader?
…On Wed, May 11, 2022 at 6:54 AM cjroebuck ***@***.***> wrote:
Search Terms
ERR_INVALID_MODULE_SPECIFIER
jest-light-runner
typescript esm
Expected Behavior
It should transpile and run the typescript file
Actual Behavior
ERR_INVALID_MODULE_SPECIFIER when running the command
Steps to reproduce the problem
See the repro below
npm run test gives TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid
module "file:///path/to/ts-node-repros/src/*tests*/foo.ts"
npm run test:working runs the tests successfully
The only difference is the --runInBand option which is implemented like
this in jest-light-runner:
***@***.***
<nicolo-ribaudo/jest-light-runner@fb70325>
I think it's something to do with dynamic import and perhaps the
ts-node/esm loader is not being invoked in this case or something?
Minimal reproduction
https://github.com/cjroebuck/ts-node-repro
Specifications
ts-node v10.7.0
node v16.14.2
compiler v4.6.4
tsconfig.json, if you're using one:
{
"extends": "ts-node/node16/tsconfig.json",
"ts-node": {
"swc": true
},
"compilerOptions": {
"module": "ESNext"
}
}
- Operating system and version: mac os monterey 12.0.1 (Apple M1 Max)
—
Reply to this email directly, view it on GitHub
<#1747>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAC35OBX77MUI7PAI6T7IVLVJOGXTANCNFSM5VUQEUXA>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Yes, it seems that esmock explicitly checks for --loader=esmock in argv, so it is not currently possible to use the I'm also using your multiloader package for this so thank you for that too :) The way I got this working in the end, with esmock and ts-node/esm loaders, was to change the command from:
to:
The first --loader=esmock keeps esmock happy, but I'm assuming just gets ignored by node, because if I switch the order of the --loader's then I'm back to the original error (ERR_INVALID_MODULE_SPECIFIER). |
Beta Was this translation helpful? Give feedback.
-
Very clever, I'm glad it's working. I expect things may change as soon as node releases support for chaining multiple loaders. They have already merged the PR, so I expect that will be available in the next node release. Then you'll be able to pass Is this a bug in esmock? It should support Given that this is not a bug report nor a feature request, but is rather a useful how-to guide, I'm going to convert it to a Discussion. |
Beta Was this translation helpful? Give feedback.
-
@cjroebuck this is awesome and NODE_OPTIONS support was added to esmock I tried making a little test similar to yours but didn't succeed. Probably I need to follow your example precisely in order for it work |
Beta Was this translation helpful? Give feedback.
-
for anyone lurking this discussion: ts-node/esm and esmock do work correctly as seen here. My current questions relate to the jest runner specifically. |
Beta Was this translation helpful? Give feedback.
-
This was originally a bug report but has been converted into a discussion. The original bug report is below.
When wanting to use esmock with ts-node/esm loaders, you'll need to use cspotcode/multiloader to allow node to use more than one --loader.
esmock currently explicitly checks for --loader=esmock to be in process.execArgv, so this also needs to be passed to node as a command line argument. It's not currently possible to use NODE_OPTIONS to run esmock alongside multiloader, though there is an issue for that.
To get esmock and ts-node/esm to play nicely together you can use something like:
node --loader=esmock --loader @cspotcode/multiloader/compose?esmock,ts-node/esm
note that esmock is specified twice, once on it's own --loader and again inside the multiloader comma separated list of loaders. As mentioned above, this is mainly to get around the check in esmock, and is eventually ignored by node in favour of the second --loader.
This trick along with many others has helped me to get some jest tests working with typescript, esmodules and module mocking after many hours of frustration/pulling my hair out.
A repo demonstrating such a setup with jest, typescript, esm and mocks is at:
https://github.com/cjroebuck/jest-typescript-esmodules-esmock
Original Bug Report Below
Search Terms
ERR_INVALID_MODULE_SPECIFIER
jest-light-runner
typescript esm
Expected Behavior
It should transpile and run the typescript file
Actual Behavior
ERR_INVALID_MODULE_SPECIFIER when running the command
Steps to reproduce the problem
See the repro below
npm run test
gives TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "file:///path/to/ts-node-repros/src/tests/foo.ts"npm run test:working
runs the tests successfullyThe only difference is the --runInBand option which is implemented like this in jest-light-runner:
nicolo-ribaudo/jest-light-runner@fb70325
I think it's something to do with dynamic import and perhaps the ts-node/esm loader is not being invoked in this case or something?
Minimal reproduction
https://github.com/cjroebuck/ts-node-repro
Specifications
ts-node v10.7.0
node v16.14.2
compiler v4.6.4
tsconfig.json, if you're using one:
Beta Was this translation helpful? Give feedback.
All reactions