swc-project / swc-node

Faster ts-node without typecheck
MIT License
1.68k stars 67 forks source link

Using `@swc-node/register` with Mocha leads to errors #784

Closed martinslota closed 1 week ago

martinslota commented 1 month ago

When Mocha is used to run test files written in Typescript along with --import=@swc-node/register/esm-register, the following kind of error gets thrown:

ReferenceError: require is not defined in ES module scope, you can use import instead
    at file:///path/to/swc-node-register-mocha-bug/node_modules/mocha/bin/mocha.js:9:29
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

A specific minimal-ish reproduce can be found in this repository. In case that reproduce does not use @swc-node/register as it is intended to be used, I'm happy to adjust it and potentially close this issue.

This issue relates to https://github.com/swc-project/swc-node/issues/762 which got fixed initially, and the fix did work, but the subsequent 1.9.1 release of @swc-node/register led to this other error.