Open joeymalvinni opened 2 years 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.
@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)?
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 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.
I use v0.4.9
/ Try to downgrade for now. It seems I got the problem reason.
@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.
Not very much about 45 commits
import chess = require('chess'); // WRONG!!!
import chess from 'chess'; // CORRECT!
// You can import create function directly
import { create, createSimple } from 'chess';
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.
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.
@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 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 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?
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
Also have this exact problem 🙃
In the README examples, the module
chess
is imported usingrequire
. However, this module is an ES6 module and doesn't allow itself to berequire
d (at least for me).This is the error I get when trying to require the module:
And this is the code used to generate this error:
Do the examples need to be updated, or am I doing something wrong?