question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

node-chess cannot be imported using common-js syntax

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
TimMikeladzecommented, Dec 28, 2022

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

0reactions
brozephcommented, Dec 1, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

CommonJS vs. ES Modules: Modules and Imports in NodeJS
In NodeJS each .js file is handled as a separate CommonJS module. ... In this case, we cannot use the default import syntax....
Read more >
Node.js now supports named imports from CommonJS ...
This syntax allows you to only import specific named exports from an ES module – in the example code above we didn't import...
Read more >
SyntaxError: Cannot use import statement outside a module
For similar functionality in CommonJS, see import(). To make Node.js treat your file as an ES module, you need to (Enabling):.
Read more >
Node Modules at War: Why CommonJS and ES ... - Code Red
ESM scripts can import CJS scripts, but only by using the “default import” syntax import _ from 'lodash' , not the “named import”...
Read more >
Using ES modules with CommonJS modules in Node.js
You can only use import and export in an ES module. Specifically, this means you can only use import and export in a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found