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.

TensorFlow.js cannot be bundled because it imports a module `seedrandom` which is actually a CommonJS module

See original GitHub issue

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

github_iconTop GitHub Comments

4reactions
jxmorris12commented, Apr 29, 2021

I had this issue on some system importing tfjs from a webworker. It was easy to fix: npm i seedrandom --save

1reaction
tafsiricommented, Oct 1, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Upgrading to TensorFlow.js 3.0
We provide support for creating custom tfjs modules to support producing size optimized browser bundles. Ship less JavaScript to your users.
Read more >
Could not find a declaration file for module 'module-name'. ...
In Node.js everything works fine, but TypeScript: import {Injector} from '@ts-stack/di' ...
Read more >
How to Get Reproducible Results with Keras
Seed Random Numbers with the TensorFlow Backend. Keras does get its source of randomness from the NumPy random number generator, so this must...
Read more >
Merge branch 'master' into util-patch-1 · 3dc7995790 - tfjs ...
tfjs-models - Pretrained models for TensorFlow.js. ... resolved "https://registry.yarnpkg.com/is-module/-/is-module-1.0.0.tgz# ...
Read more >
Welcome to Everything.js | Everything.js
Packs many modules into a few bundled assets. Code Splitting allows for loading parts of the application on demand. Through “loaders”, modules can...
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