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.

Scala.js runtime error: require is not a function

See original GitHub issue

Problem

In Scala.js, Coursier’s NPM dependencies are loaded through js.Dynamic.global.require("..."). This is problematic for two reasons:

  • Incompatibility with ES modules.

    Scala.js projects depending on Coursier should have the choice between generating ES module imports or CommonJS requires, but the current implementation of Coursier contains hard-coded calls to require. This means that Scala.js projects targeting ES modules will also generate require calls, which won’t work in the browser (unless the bundle is passed through a bundler like Webpack).

  • global.require may be undefined depending on how the JS code is loaded.

    For instance, running console.log(global.require) prints undefined in a JS file (loaded using node test.js) but prints the require function in a Node.js REPL. See this StackOverflow question.

These two reasons both lead to the same runtime error: TypeError: $g.require is not a function.

Solution

There are two possible solutions for this:

  1. Use @JSImport over js.Dynamic.global.require.
  2. Update to Scala.js 1.x and use @JSGlobal to access require instead of global.require.

I think solution 1 is probably preferable to solution 2, as Scala.js projects can then target the module system they need. While I think Coursier should eventually move to Scala.js 1.x, solution 2 requires getting all of Coursier’s dependencies to switch to 1.x, which may take time.

Workaround until the solution is implemented

If anyone else needs to work around the issue for now, the hack I’ve found is to use Webpack to bundle my Scala.js output with a JS file containing eval("global.require = require"). Note that you should then very carefully determine if the Coursier dependencies (xhr2, cheerio, xmldom and sax) should be bundled too (for the Web) or just be available in node_modules (for Node.js).

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
MaximeKjaercommented, Jun 4, 2020

I updated my project to Scala.js 1.1.0 and Coursier v2.0.0-RC6-21, and can confirm that #1715 fixes the problem. I’ll be closing this issue – thank you!

1reaction
alexarchambaultcommented, Apr 28, 2020

Otherwise, since scalajs-react will very soon be releasing for 1.0, it may also be acceptable to just let this issue be the documentation for the workaround in the meantime, and solve the issue by upgrading to 1.0.

Let’s do this for now then. I don’t really know how many people depend on / use coursier via its Scala.JS artifacts, they’ll find this issue if needed, yes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ScalaJS Uncaught TypeError: $g.require is not a function
When I access http://localhost:12345/target/scala-2.11/classes/index-dev.html. Uncaught TypeError: $g.require is not a ...
Read more >
"ay is not a function" when writing a property called "y ... - GitHub
I just stumbled upon this problem: I define a class in scalajs, export it to javascript and run it through fullOpt.
Read more >
How To Fix ReferenceError require is not defined in JavaScript
In this case, check your package.json file for an property called type . If that is set to module , ES6 modules will...
Read more >
scala-js/scala-js - Gitter
Getting the following error when running -fastopt.js from node, works fine thru sbt run ... TypeError: $g.require is not a function
Read more >
Access to the JavaScript global scope in Scala.js
First, in any of the 4 above statements, if the referenced variable is not declared as a global variable, a ReferenceError will be...
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