Reputation: 1498
I am trying to include a generated js module in my webpage. Yet it gives me an error: Uncaught TypeError: Cannot read property 'Object' of undefined
at $g["Object"]["freeze"]($env);
The minimal example is:
File index.html
:
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
</head>
<body>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script type="module">
import { foo } from './foo.js';
console.log(foo());
</script>
</body>
</html>
File foo.js
:
'use strict';
/* Scala.js runtime support
* Copyright 2013 LAMP/EPFL
* Author: Sébastien Doeraene
*/
/* ---------------------------------- *
* The top-level Scala.js environment *
* ---------------------------------- */
// Get the environment info
var $env = (typeof __ScalaJSEnv === "object" && __ScalaJSEnv) ? __ScalaJSEnv : {};
// Global scope
var $g =
(typeof $env["global"] === "object" && $env["global"])
? $env["global"]
: ((typeof global === "object" && global && global["Object"] === Object) ? global : this);
$env["global"] = $g;
$env["exportsNamespace"] = void 0;
// Freeze the environment info
$g["Object"]["freeze"]($env);
export function foo() { return "Hello"; }
But if I just copy paste the script into chrome console, then everything works as it should.
Upvotes: 0
Views: 454
Reputation: 22085
You are hitting a bug in Scala.js 0.6.x: https://github.com/scala-js/scala-js/issues/3677. The global detection code in var $g = ...
is incorrect when used in ES modules outside of Node.js (e.g., in a browser) and fails to discover the correct global object.
The issue mentions a workaround: add the following <script>
tag in your HTML file (before the one that imports the Scala.js code):
<script type="text/javascript">
var __ScalaJSEnv = { global: window };
</script>
This will explicitly tell Scala.js what the global object is, i.e., window
.
The issue is fixed in Scala.js 1.x, whose RC1 was released two days ago.
Upvotes: 1