chess.js
chess.js copied to clipboard
chess.js not working in a typescript environment
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
Same issue
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 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.```
@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
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.
@jhlywa Awesome! I'm happy to hear you're converting this repo to TypeScript. 🙂
I am still getting the ERR_REQUIRE_ESM error on 1.0.0-beta.4
Here's a repo to reproduce the problem: https://github.com/neofight78/import-issue
@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.
@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?
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.
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 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.