chevrotain icon indicating copy to clipboard operation
chevrotain copied to clipboard

Typescript issue ERR_PACKAGE_PATH_NOT_EXPORTED

Open xcarpentier opened this issue 1 year ago • 5 comments

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

xcarpentier avatar Feb 16 '24 14:02 xcarpentier

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

msujew avatar Feb 16 '24 15:02 msujew

Thanks @msujew for your reply.

I tried with the latest 11.0.3.

I downgraded to the 10.5.0 without issue.

xcarpentier avatar Feb 16 '24 16:02 xcarpentier

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.

msujew avatar Feb 16 '24 17:02 msujew

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.

xcarpentier avatar Feb 16 '24 18:02 xcarpentier

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.

msujew avatar Feb 16 '24 19:02 msujew

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

xcarpentier avatar Feb 23 '24 18:02 xcarpentier

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

msujew avatar Feb 23 '24 20:02 msujew

@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.

gentle-noah avatar Feb 26 '24 20:02 gentle-noah

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 avatar Feb 26 '24 23:02 msujew

@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).

msujew avatar Feb 26 '24 23:02 msujew

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.

gentle-noah avatar Mar 13 '24 00:03 gentle-noah