node-chess cannot be imported using common-js syntax
See original GitHub issueIn the README examples, the module chess
is imported using require
. However, this module is an ES6 module and doesn’t allow itself to be require
d (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:
- Created a year ago
- Comments:15 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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@SamTwining thank you for your checking on this… I ended up adding an
examples
folder with a file namedusage.go
. I performed an npm link to create the chess module symlink globally for my node install and then ran the file…I was able to observe this working, and I’m running node
v16.18.1
at this time. Does the above work for you?