Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chess.js not working in a typescript environment #335

Closed
mickev36 opened this issue Jun 21, 2022 · 13 comments
Closed

chess.js not working in a typescript environment #335

mickev36 opened this issue Jun 21, 2022 · 13 comments

Comments

@mickev36
Copy link

mickev36 commented Jun 21, 2022

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

@AdditionAddict
Copy link

Same issue

@jhlywa
Copy link
Owner

jhlywa commented Jun 25, 2022

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 #319) before this release becomes official.

@rektadult
Copy link

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

@jhlywa
Copy link
Owner

jhlywa commented Jun 26, 2022

@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

@rektadult
Copy link

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.

@LandonSchropp
Copy link

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

@neofight78
Copy link
Contributor

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

@neofight78
Copy link
Contributor

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

@jhlywa
Copy link
Owner

jhlywa commented Mar 21, 2023

@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
Copy link
Contributor

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

@jhlywa
Copy link
Owner

jhlywa commented Apr 18, 2023

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.

@pnb
Copy link

pnb commented Apr 21, 2023

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!

@jhlywa
Copy link
Owner

jhlywa commented Apr 26, 2023

@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 (#318 (comment)). I'll investigate this soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants