chess.js icon indicating copy to clipboard operation
chess.js copied to clipboard

chess.js not working in a typescript environment

Open mickev36 opened this issue 2 years ago • 6 comments

Hello,

The chess.js module doesn't seem to work in a Typescript environment. (ERR_REQUIRE_ESM when importing) Typescript by default outputs code with the "commonjs" module type. Unfortunately, chess.js only seem to support the ESM module type. (Working with esnext as output module type is not really possible at the moment, see https://www.tsmean.com/articles/learn-typescript/typescript-module-compiler-option/)

I've made a minimal repository to reproduce the issue : https://github.com/mickev36/chessjsissue (run it with npx ts-node main.ts)

Switching a whole project to ESM modules just for chess.js seems problematic, since it would break pretty much all the other dependencies in projects. Transpiling the project also seems overkill for the task.

Can you please provide an implementation which works with a commonjs module type ? Or a way around it ?

Thanks

mickev36 avatar Jun 21 '22 16:06 mickev36

Same issue

AdditionAddict avatar Jun 24 '22 15:06 AdditionAddict

Hi and thanks for the report. There's a new typescript version of chess.js in the dev branch that's about 95% feature complete. It's slated to be version 1.0.0.

You can try it out by running the (untested) commands below:

$ npm install https://github.com/jhlywa/chess.js#dev
$ cd node_modules/chess.js
$ npm run build

Note that the 1.0.0 API uses camelCase instead of snake_case function calls. There are still a few planned changes (see https://github.com/jhlywa/chess.js/issues/319) before this release becomes official.

jhlywa avatar Jun 25 '22 13:06 jhlywa

@jhlywa i ran the above commands and got these errors (i recognize you mentioned it is untested):

npm run build

> [email protected] build
> tsc --build

src/chess.ts:1817:11 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<Square, number>'.
  No index signature with a parameter of type 'string' was found on type 'Record<Square, number>'.

1817           Ox88[from] == moves[i].from &&
               ~~~~~~~~~~

src/chess.ts:1818:11 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<Square, number>'.
  No index signature with a parameter of type 'string' was found on type 'Record<Square, number>'.

1818           Ox88[to] == moves[i].to &&
               ~~~~~~~~

src/chess.ts:1830:13 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Record<Square, number>'.
  No index signature with a parameter of type 'string' was found on type 'Record<Square, number>'.

1830             Ox88[to] == moves[i].to &&
                 ~~~~~~~~


Found 3 errors.```

rektadult avatar Jun 26 '22 03:06 rektadult

@933459 Perhaps you're running with an older version of typescript? I've only tested with 4.4.x and greater.

Try adding a call to npm install prior to calling npm run build, so

$ npm install https://github.com/jhlywa/chess.js#dev
$ cd node_modules/chess.js
$ npm install
$ npm run build

jhlywa avatar Jun 26 '22 13:06 jhlywa

thanks @jhlywa , yes i was accidentally using typescript 3.4. upgrading to typescript@latest allows the dev version of chess.js to build correctly per the directions above. appreciate your time on this.

rektadult avatar Jun 27 '22 21:06 rektadult

@jhlywa Awesome! I'm happy to hear you're converting this repo to TypeScript. 🙂

LandonSchropp avatar Jul 16 '22 18:07 LandonSchropp

I am still getting the ERR_REQUIRE_ESM error on 1.0.0-beta.4

neofight78 avatar Mar 20 '23 22:03 neofight78

Here's a repo to reproduce the problem: https://github.com/neofight78/import-issue

neofight78 avatar Mar 21 '23 07:03 neofight78

@neofight78 Thanks for the report. I'll spend a few days looking into supporting both ESM and commonjs. Worst case scenario, I could push an additional package/branch to NPM w/ commonjs support.

jhlywa avatar Mar 21 '23 11:03 jhlywa

@neofight78 Thanks for the report. I'll spend a few days looking into supporting both ESM and commonjs. Worst case scenario, I could push an additional package/branch to NPM w/ commonjs support.

Thank you 🙏 It's not an area I'm super familiar with otherwise I would offer to help. Perhaps tsup could be of some use?

neofight78 avatar Mar 21 '23 11:03 neofight78

Version v1.0.0-beta.5 reverts the library back to CommonJS modules. This should make things significantly easier for node developers. The latest has been pushed to npm.

jhlywa avatar Apr 18 '23 23:04 jhlywa

On the other hand, I cannot for the life of me figure out how to import the CommonJS beta5 version in a ES6 module project. I'm using beta4 right now, which works great in both browser and NodeJS (because I use modules for everything including Node).

It seems like it may not even be possible for beta5 to work in the browser since it is CommonJS, at least not without Webpack or similar. Though I'm not an expert. I certainly couldn't get it working though. :(

Perhaps it is possible to publish separate builds for each version? Not sure if NPM has a nice way to allow that, or if there is a more elegant solution. Sorry to complicate the situation further, and thanks for this great work!

pnb avatar Apr 21 '23 23:04 pnb

@pnb Thanks for the feedback and sorry for the breaking changes. @GabrielDelepine mentioned supporting both ESM and CommonJS should be possible via the exports field in package.json (https://github.com/jhlywa/chess.js/pull/318#issuecomment-1516036393). I'll investigate this soon.

jhlywa avatar Apr 26 '23 00:04 jhlywa