node-chess icon indicating copy to clipboard operation
node-chess copied to clipboard

node-chess cannot be imported using common-js syntax

Open joeymalvinni opened this issue 1 year ago • 16 comments

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?

joeymalvinni avatar Sep 20 '22 04:09 joeymalvinni

Hello. I use require like here

const chess = require('chess')

and everything work fine.

Piterden avatar Sep 21 '22 09:09 Piterden

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.

lukasm0 avatar Sep 21 '22 13:09 lukasm0

@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 avatar Sep 22 '22 05:09 joeymalvinni

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.

joeymalvinni avatar Sep 22 '22 05:09 joeymalvinni

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

lukasm0 avatar Sep 23 '22 09:09 lukasm0

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

Piterden avatar Sep 23 '22 10:09 Piterden

@Piterden [email protected] 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.

lukasm0 avatar Sep 23 '22 12:09 lukasm0

Not very much about 45 commits

Piterden avatar Sep 23 '22 12:09 Piterden

import chess = require('chess'); // WRONG!!!
import chess from 'chess';       // CORRECT!

// You can import create function directly
import { create, createSimple } from 'chess';

Piterden avatar Sep 23 '22 12:09 Piterden

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.

lukasm0 avatar Sep 23 '22 12:09 lukasm0

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.

Piterden avatar Sep 23 '22 12:09 Piterden

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

brozeph avatar Sep 26 '22 18:09 brozeph

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

SamTwining avatar Nov 26 '22 15:11 SamTwining

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

brozeph avatar Dec 01 '22 18:12 brozeph

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

TimMikeladze avatar Dec 28 '22 04:12 TimMikeladze

Also have this exact problem 🙃

TinyCamera avatar Feb 23 '23 03:02 TinyCamera