brozeph / node-chess

A simple node.js library for parsing and validating chess board position with an algebraic move parser
https://brozeph.github.io/node-chess
MIT License
142 stars 31 forks source link

node-chess cannot be imported using common-js syntax #84

Open joeymalvinni opened 2 years ago

joeymalvinni commented 2 years ago

In the README examples, the module chess is imported using require. However, this module is an ES6 module and doesn't allow itself to be required (at least for me).

This is the error I get when trying to require the module:

var Chess = require('chess').Chess;
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\Users\Joey\Programming\Node.js\node_modules\chess\src\main.js from C:\Users\Joey\Programming\Node.js\chess.js not supported.
Instead change the require of main.js in C:\Users\malvi\Programming\Node.js\ChessBot\chessjstest.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\Users\Joey\Programming\Node.js\chessjs.js :3:13) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v17.0.

And this is the code used to generate this error:

const Chess = require('chess').Chess;

Do the examples need to be updated, or am I doing something wrong?

Piterden commented 1 year ago

Hello. I use require like here

const chess = require('chess')

and everything work fine.

lukasm0 commented 1 year ago

This doesn't seem to wort for me either. I also tried import chess = require('chess'); and import * as chess from 'chess'; but no success so far.

joeymalvinni commented 1 year ago

@lukasm0 You cannot mix ESM and CommonJS imports.

import chess = require('chess'); will never work because you aren't initializing a variable while using a require, and you aren't specifying the module while using an import. However, it's interesting that ESM imports do not work for you. Does import chess from 'chess' work for you?

Also, what version are of Node.js are you using (and version of Chess.js)?

joeymalvinni commented 1 year ago

Hello. I use require like here

const chess = require('chess')

and everything work fine.

What version of Node.js and chess.js are you using?

I am using version 17.0.1 of Node.js and version 1.0.2 of node-chess.

lukasm0 commented 1 year ago

@joeymalvinni I'm using NestJs, so the version is called "@nrwl/node: 14.7.8" but I'm pretty sure it should be basically the same as the normal "node:14.7.8" I'm using "chess": "^1.0.2"

The error message I'm getting when using import chess from 'chess':

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/lukas/chess/node_modules/chess/src/main.js from /home/lukas/chess/dist/apps/api/main.js not supported.
Instead change the require of /home/lukas/chess/node_modules/chess/src/main.js in /home/lukas/chess/dist/apps/api/main.js to a dynamic import() which is available in all CommonJS modules.

As far as I understand it, dynamic import means using import as an async function, so I tried this:

(async () => {
      const chess = await import('chess');
      const gameClient = chess.create();
      console.log('gameClient', gameClient);
    })().catch((err) => console.error(err));

but it throws the same error.

Piterden commented 1 year ago

I use v0.4.9/ Try to downgrade for now. It seems I got the problem reason.

lukasm0 commented 1 year ago

@Piterden chess@0.4.9 works, thanks! How much am I missing out with that version?

@joeymalvinni with this chess version the correct import syntax actually seems to be import chess = require('chess');. I think this is just how typescript wants it to be and in the end it translates to const chess = require('chess') under the hood. import chess from 'chess' also works, but then you need to set "esModuleInterop": true, in your tsconfig.

Piterden commented 1 year ago

Not very much about 45 commits

Piterden commented 1 year ago
import chess = require('chess'); // WRONG!!!
import chess from 'chess';       // CORRECT!

// You can import create function directly
import { create, createSimple } from 'chess';
lukasm0 commented 1 year ago

Just out of curiosity why is import chess = require('chess'); wrong? It works, also check out this answer

I think import { create, createSimple } from 'chess'; is the most natural one and I will use this.

Piterden commented 1 year ago

What do you mean why? Because ECMAScript doesn't support that syntax. Maybe TS supports, but I highly not recommend to use TS in development generally.

brozeph commented 1 year ago

@joeymalvinni and @lukasm0 in v1.1.0 I just modified the module export syntax which appears to resolve this issue in my local testing. I modified the readme.md as well to denote use of import instead of the Commonjs require. Want to give this a shot?

SamTwining commented 1 year ago

@brozeph Not OP but can confirm this error is still occuring. I'm using named imports yet ts-node is still detonating as if it's a require which feels like something wonky is going on.

brozeph commented 1 year ago

@SamTwining thank you for your checking on this... I ended up adding an examples folder with a file named usage.go. I performed an npm link to create the chess module symlink globally for my node install and then ran the file...

git clone https://github.com/brozeph/node-chess.git
cd node-chess
npm link
cd examples
npm link chess
node usage.js

I was able to observe this working, and I'm running node v16.18.1 at this time. Does the above work for you?

TimMikeladze commented 1 year ago

FYI in case it helps anyone out: in order to unblock myself I published @tmikeladze/chess which can be imported in a common js project. It has the changes from #88

TinyCamera commented 1 year ago

Also have this exact problem 🙃