Either export for Node/CommonJS or RequireJS, not both
See original GitHub issueAt the moment the following happens:
/* export Chess object if using node or any other CommonJS compatible
* environment */
if (typeof exports !== 'undefined') exports.Chess = Chess;
/* export Chess object for any RequireJS compatible environment */
if (typeof define !== 'undefined') define( function () { return Chess; });
When using only Node this works fine, because the second statement is not executed. When using Webpack, however, both statements are executed (because it supports both if I’m correct), which means that the first statement is overwritten (at least it seems like that) and, hence, is therefore not longer compatible with code that is written for Node. And the code works without Webpack. Note that I don’t have this problem when using chess.js as a direct dependency, only when chess.js is an indirect dependency.
Therefore, I would suggest to turn these two separate ifs into in an if-else.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Using Node.js require vs. ES6 import/export
Babel converts import and export declaration to CommonJS ( require / module.exports ) by default anyway. So even if you use ES6 module...
Read more >RequireJS in Node
RequireJS in Node can only load modules that are on the local disk -- fetching modules across http, for instance, is not supported...
Read more >Difference between node.js require and ES6 import and ...
javascript ; Require is Non-lexical, it stays where they have put the file. Import is lexical, it gets sorted to the top of...
Read more >CommonJS modules | Node.js v19.3.0 Documentation
Using package subpath exports or subpath imports can provide the same containment organization benefits as folders as modules, and work for both require...
Read more >JavaScript Require vs. Import
exports is used to export CommonJS modules, and import function is used to include modules into separate files. Although CommonJS modules are ...
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
My current (ugly) workaround:
It turns out there’s an easy way to fix this with Webpack. Simply disable the AMD parser for chess.js in your Webpack config:
Now you can run
in both your Node code and your Webpack-bundled code.