Chevrotain / chevrotain

Parser Building Toolkit for JavaScript
https://chevrotain.io
Apache License 2.0
2.51k stars 206 forks source link

Typescript issue ERR_PACKAGE_PATH_NOT_EXPORTED #2018

Closed xcarpentier closed 9 months ago

xcarpentier commented 9 months ago

Seems to not working with typescript anymore ?

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined in /Users/XXX/quick-start/node_modules/chevrotain/package.json

msujew commented 9 months ago

@xcarpentier Which version of chevrotain are you pulling? This should be resolved with the latest version.

xcarpentier commented 9 months ago

Thanks @msujew for your reply.

I tried with the latest 11.0.3.

I downgraded to the 10.5.0 without issue.

msujew commented 9 months ago

Oh, wait. Your typescript version complains that there's no main export defined. Could it be, that your TypeScript compiler version is kind of ... very old? Note that Chevrotain has dropped the CJS support with version 11, so it might be that it doesn't even run in your project without a special bundling setup.

xcarpentier commented 9 months ago

The Typescript version is 5.3.3. You can see my project it here When I changes the version to latest that's not working anymore, test failed because not finding chevrotain package.

msujew commented 9 months ago

It's likely due to this config:

https://github.com/xcarpentier/dsl/blob/c5ee4d13eb9ad51f1c78484a28eb43205c039230/tsconfig.json#L9-L10

Since Chevrotain is ESM only, it cannot be imported as commonjs. TypeScript will attempt to do that and fail.

xcarpentier commented 9 months ago

Do you have any example of configuration on github. I'm not able to make it work with 11 version :/ ?

msujew commented 9 months ago

@xcarpentier We use Chevrotain@11 in Langium, see here for our config.

gentle-noah commented 9 months ago

@xcarpentier - were you able to get this working? We are having the same issue. Switching out to a very custom tsconfig is generally undesirable and also causes TS to complain a lot when following the base examples from the quick start guide.

msujew commented 9 months ago

Switching out to a very custom tsconfig is generally undesirable

@gentle-noah Note that nothing Chevrotain requires is in any way special. ESM seems to be the way forward for most packages and the required tsconfig settings to import ESM code have been available for years now.

Aside from that, can you provide a reproducible example to showcase the issue you're experiencing?

msujew commented 9 months ago

@xcarpentier By the way, I've taken another look at your repo. The issue doesn't seem to be that TypeScript complains about chevrotain, but rather that your JavaScript code isn't able to run require('chevrotain'). Which is expected, given that Chevrotain cannot be imported in cjs contexts. You need to use import * as chevrotain from 'chevrotain' and start node with the --module option (or set "type": "module" in your package.json).

gentle-noah commented 8 months ago

For anyone that stumbled on this issue, ee ended up just adopting v10.5 and everything works great. Having to juggle ESM only in more complex stacks like one built in an NX monorepo leads to too many TS and import issues. Being able to avoid dynamic imports also decreased parsing time from ~2.1s to 28ms on average.