Closed dhowe closed 5 months ago
Given the stack trace, Mocha runs your code as CommonJS, but Chevrotain is now an ESM only library. It cannot be imported via require
.
I use import
throughout the code base, and this problem only occurs when I add that one line, importing CstParser
:
import Inflector from './inflector';
import RandGen from './randgen';
import Util from './util';
import { Query } from "mingo"
// adding this line triggers the issue
import { CstParser } from "chevrotain"
Copying from your supplied stack trace:
at Module.require (node:internal/modules/cjs/loader:1021:19)
at require (node:internal/modules/cjs/helpers:103:18)
at /Users/dhowe/git/ritajs.v3/test/before.js:19:25
It looks like it still uses require/cjs internally. Might be more related to mocha than nodejs directly.
Right -- I guess what I'm trying to figure out is what is different about chevrotain that triggers this issue, while none of the many other libraries I'm using do
Chevrotain is probably different in the sense that it only has an import
export. Others such as mingo
also export a require
, which is used in this context. I would recommend vitest for testing an ESM app by the way. I assume that babel + mocha is messing with the import and transforms it into a require
.
Hmmm, ok vitest looks like it would be a good option if I didn't already have many hundreds of mocha tests
Afaik, vitest should be a drop-in replacement for mocha. You just need to adjust your imports/globals.
This is a related question: I want to bundle my library (which uses Chevrotain) as cjs for node. Is this possible given that chevrotain is now ESM only?
Or, would, you recommend, at the end of 2023, that newly-created libraries also be ESM only?
A reddit thread on the question
@dhowe You could call Chevrotain (in it's current state) CJS compatible ESM code. Bundlers such as esbuild can compile it back into cjs without any issues. I use it like that as well when bundling for vscode extensions.
I feel like the ecosystem is still in a transitional period. My company has recently migrated to releasing newer major versions of libraries as ESM-only. However, adoption for those new versions isn't high enough yet to get a good grip on whether people can deal well with it. Generally, I've seen most libraries currently exporting both CJS and ESM, which seems to work well. I imagine them eventually dropping CJS though, such as Chevrotain did.
thanks for this @msujew - do you mind sharing your esbuild
config options for bundling Chevrotain as commonjs (I'm using tsup
which is built on esbuild
) ??
@dhowe We're using esbuild to build language servers (using Chevrotain as the parser engine) for vscode, which only accepts cjs right now. See here for our esbuild config.
thank you @msujew
Greetings, I have a project to which I am trying to add chevrotain. I have been running mocha tests as follows:
After installing chevrotain with npm (I see "^11.0.3" in my package.json ), I get the error below when I run the test command. The only code I have added is the a new file with this one line:
Line 19 of
before.js
, from which the error appears to originate, is as follows: