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

Make native Node ESM support configurable #4853

Closed
tbranyen opened this issue Mar 18, 2022 · 5 comments
Closed

Make native Node ESM support configurable #4853

tbranyen opened this issue Mar 18, 2022 · 5 comments
Labels
status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: feature enhancement proposal

Comments

@tbranyen
Copy link

tbranyen commented Mar 18, 2022

Is your feature request related to a problem or a nice-to-have?? Please describe.

Relates to: #4038

Currently unable to use "type": "module" in my mono-repo package as then Mocha runs all tests assuming native node, instead of using the configured @babel/register extensions loader. There seems to be a conflation that because a package is designated as native Node ESM, that means the tests should run that way. There is a big distinction in my case.

I import my code using the extension-less form:

import { innerHTML } from './inner-html';

(See build failure in Additional context below)

Describe the solution you'd like

I would like to see an option to be able to disable Mocha's Node interpretation, regardless of .mjs or "type": "module" in the root package.json.

Something like:

--disable-esm

Describe alternatives you've considered

  • Disabling the feature to import Node ESM from the root of my project
  • Converting all my imports to have an extension

Additional context

λ NODE_ENV=test+cov nyc -r=lcov mocha test/_setup test test/integration/*.js


Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/tim/git/diffhtml/packages/diffhtml/lib/util/types' imported from /home/tim/git/diffhtml/packages/diffhtml/lib/util/pool.js
    at finalizeResolution (internal/modules/esm/resolve.js:285:11)
    at moduleResolve (internal/modules/esm/resolve.js:708:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:798:11)
    at Loader.resolve (internal/modules/esm/loader.js:100:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:246:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:47:40)
    at link (internal/modules/esm/module_job.js:46:36)
@tbranyen tbranyen added the type: feature enhancement proposal label Mar 18, 2022
@tbranyen tbranyen changed the title Disable native Node ESM support Make native Node ESM support configurable Mar 18, 2022
@juergba
Copy link
Member

juergba commented Mar 19, 2022

@tbranyen I haven't really understood your request, but it seems very weird to me to configure a project as ESM (via .mjs or type: module) and then instruct Mocha to do the opposite.

Have you noted this in Babel's docs?

Note: @babel/register does not support compiling native Node.js ES modules on the fly, since currently there is no stable API for intercepting ES modules loading.

Have you tried this: babel-register-esm?

@giltayar cc

@tbranyen
Copy link
Author

@juergba ah I missed that in the babel register docs 👍. This may be a non-issue for Mocha in that case.

@giltayar
Copy link
Contributor

If you'd like to disable ESM, just don't put "type": "module" in the package.json, and all will be well. Putting that in the package.json signals to Node.js that you want all .js files to be ESM native.

But the ecosystem is slowly but inexorably moving to native ESM, so I would definitely investigate moving to ESM. If you need on the fly babel transpilation, like @babel/register does, then use the babel-register-esm plugin, as @juergba suggested:

npm install babel-register-esm
mocha --loader babel-register-esm testfile.js

@tbranyen
Copy link
Author

tbranyen commented Mar 19, 2022

@giltayar @juergba I've been able to confirm my problem using plain Node and @babel/register as well (no mocha). Shame, because all my other tooling still works since they don't "shadow" the node process like Mocha does. I use the "type": "module" attribute value since I have some files in the root that re-export from a safe Node ESM directory:

export * from './dist/es/lite.js';

The rest of the project is not necessarily "Node ESM". Given that this is not a uniquely Mocha problem, I think it's safe to close. I will investigate using a Node ESM loader to allow extension-less imports as that seems to be the only real blocker ATM.

@tbranyen
Copy link
Author

Looks like Node provides an option to support extension-less imports via https://nodejs.org/api/esm.html#customizing-esm-specifier-resolution-algorithm: --experimental-specifier-resolution=node.

I set the following config in .mocharc.json, but haven't had success yet. Will update if I can get it working as this would completely solve the original ask.

{
  "node-option": [
    "experimental-specifier-resolution=node"
  ]
}

@juergba juergba added the status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior label Mar 21, 2022
@juergba juergba closed this as completed Mar 22, 2022
Offroaders123 added a commit to Offroaders123/lz4js that referenced this issue Mar 21, 2024
I'm not sure how to use or configure ESLint yet, I have yet to try it out myself. I'm going to upgrade their versions to the latest releases though.

This actually made things work very easily as ESM out of the box! Extra awesome! I thought I was going to have to configure a bunch more to get it working. Turns out Mocha supports it, it's Jest that doesn't. Or something along those lines when I tried it last. I think that was for JSMediaTags I was getting stuck with those kinds of issues.

I didn't end up using these links to set things up, but it helped me get to the right place. The plain dependencies fixed the issue itself, it seems.

mochajs/mocha#4853
https://daveschinkel.medium.com/setting-up-mocha-js-to-run-typescript-42f3099450eb
https://stackblitz.com/edit/node-pawwmm
https://mochajs.org/#serial-mode
https://stackoverflow.com/questions/54805319/mocha-error-no-test-files-found-test-npm-err-test-failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: wontfix typically a feature which won't be added, or a "bug" which is actually intended behavior type: feature enhancement proposal
Projects
None yet
Development

No branches or pull requests

3 participants