TensorFlow.js cannot be bundled because it imports a module `seedrandom` which is actually a CommonJS module
See original GitHub issueSystem information:
- Have I written custom code (as opposed to using a stock example script provided in TensorFlow.js): No
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS 10.15.6
- TensorFlow.js installed from (npm or script link): npm
- TensorFlow.js version (use command below):
2.4.0
- Browser version: recent Chrome
Describe the current behavior: ES module bundler complains when bundling TensorFlow.js, because it imports a module seedrandom
which is actually a CommonJS module.
Describe the expected behavior: Running TensorFlow.js through an ES module bundler should not generate errors.
Standalone code to reproduce the issue: https://github.com/jameshfisher/tfjs-seedrandom-es-module-bug
Context: I want to use TensorFlow.js in the browser. I’m using rollup to bundle my app for the browser.
The TensorFlow.js source imports from seedrandom
in several places. This comes through in the ES modules distributed via npm.
To find the module seedrandom
imported by TensorFlow, I’m using the plugin @rollup/plugin-node-resolve
. This does find the node_modules/seedrandom
package.
Unfortunately, @rollup/plugin-node-resolve
does not find an ECMAScript module in the seedrandom package. So instead, it decides to bundle the seedrandom index.js
. But this is a CommonJS module, not an ES module.
As a result, rollup gives me errors like alea is not exported by node_modules/seedrandom/index.js
, and the browser gives me errors like require is not defined
.
I think the right solution is for the seedrandom package to provide an ES module. I noted this here. The alternative is for TensorFlow to stop importing from a package that doesn’t provide an ES module.
(You might say “well you just need to use a CommonJS->ES transpiler, like @rollup/plugin-commonjs”. I’m reluctant to do so, because this transformation is gross, and in general, impossible.)
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:7 (2 by maintainers)
Top GitHub Comments
I had this issue on some system importing tfjs from a webworker. It was easy to fix:
npm i seedrandom --save
@jameshfisher I saw on the issue you filed that David said he’d be willing to accept pull requests for this on the seedrandom side, so if you do end up making a PR there let us know and we can upgrade to an es friendly release!