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.

Either export for Node/CommonJS or RequireJS, not both

See original GitHub issue

At 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:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
maxwellhaydncommented, Jun 15, 2020

My current (ugly) workaround:

const { isNode } = require('browser-or-node');

let Chess;

if (isNode) {
    const chess = require('chess.js');
    Chess = chess.Chess;
}
else {
    Chess = require('chess.js');
}
1reaction
maxwellhaydncommented, Jun 20, 2020

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:

module: {
    rules: [
    ...
        {
            test: require.resolve('chess.js'),
            parser: {
                amd: false
            }
        }
    ]
}

Now you can run

const { Chess } = require('chess.js');

in both your Node code and your Webpack-bundled code.

Read more comments on GitHub >

github_iconTop 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 >

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