Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing random in Javascript causes a ReferenceError in Node #16369

Closed
ziotom78 opened this issue Dec 16, 2020 · 8 comments
Closed

Importing random in Javascript causes a ReferenceError in Node #16369

ziotom78 opened this issue Dec 16, 2020 · 8 comments

Comments

@ziotom78
Copy link
Contributor

Importing random and exporting to a Javascript file produces a ReferenceError when ran by Node.

Example

import random

echo "Hello, world!"

Current Output

Hint: used config file '/home/tomasi/Projects/Nim/config/nim.cfg' [Conf]
Hint: used config file '/home/tomasi/Projects/Nim/config/config.nims' [Conf]
...................
/home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.nim(1, 8) Warning: imported and not used: 'random' [UnusedImport]
Hint: 32869 lines; 0.376s; 39.891MiB peakmem; Debug build; proj: /home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.nim; out: /home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.js [SuccessX]
Hint: /usr/bin/node /home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.js  [Exec]
/home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.js:2506
if (globalThis.utcInstance_603981990 === undefined) {
^

ReferenceError: globalThis is not defined
    at Object.<anonymous> (/home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.js:2506:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Error: execution of an external program failed: '/usr/bin/node /home/tomasi/Documents/corsi/ray-tracing/performance-tests/intersect.nim/test.js '

Expected Output

Hello World!

Possible Solution

  • Removing import random solves the issue. However, in the script I am developing, I need to use random.

Additional Information

This happens with both the master Nim branch and with release 1.4.2.

$ /home/tomasi/Projects/Nim/bin/nim --version
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2020-12-16
Copyright (c) 2006-2020 by Andreas Rumpf

git hash: 9ef7c0182058141b81a41e32b699be8f2e42f3e9
active boot switches: -d:release

I see there have been some issues with random+JS in the past (#12418, #11772, #9999), but they seem unrelated, as the error there was an overflow.

@timotheecour
Copy link
Member

timotheecour commented Dec 16, 2020

can't reproduce

  • also tried with: XDG_CONFIG_HOME= nim r -b:js --eval:'import random; echo 1' to avoid my user config
  • also tried with -d:nodejs
  • and with provided hash 9ef7c0182058141b81a41e32b699be8f2e42f3e9 and 1.4.2

node -v
v14.7.0

on osx.

what is node -v for you?

@ziotom78
Copy link
Contributor Author

I am using Linux Mint 20 and have installed node using apt. The version provided by Mint is the same as Ubuntu 20.04, which is older than yours:

$ node -v
v10.19.0

$ apt-cache show nodejs
Package: nodejs
Architecture: amd64
Version: 10.19.0~dfsg-3ubuntu1
Multi-Arch: foreign
Priority: extra
Section: universe/web
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian Javascript Maintainers <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 154
Depends: libc6 (>= 2.4), libnode64 (= 10.19.0~dfsg-3ubuntu1)
Recommends: ca-certificates, nodejs-doc
Suggests: npm
Conflicts: nodejs-legacy
Replaces: nodejs-legacy
Filename: pool/universe/n/nodejs/nodejs_10.19.0~dfsg-3ubuntu1_amd64.deb
Size: 61128
MD5sum: e99245e2f78b02e736f6585be7a200f2
SHA1: 8a7f18fc393650d3f0830c6cde0e0bfa7e4afa17
SHA256: 392d4ae36ca956260df71b43f7cd04c1f8928dac5e53f4132d237772b16adf4a
Homepage: http://nodejs.org/
Description-en: evented I/O for V8 javascript - runtime executable
 Node.js is a platform built on Chrome's JavaScript runtime for easily
 building fast, scalable network applications. Node.js uses an
 event-driven, non-blocking I/O model that makes it lightweight and
 efficient, perfect for data-intensive real-time applications that run
 across distributed devices.
 .
 Node.js is bundled with several useful libraries to handle server
 tasks:
 .
 System, Events, Standard I/O, Modules, Timers, Child Processes, POSIX,
 HTTP, Multipart Parsing, TCP, DNS, Assert, Path, URL, Query Strings.
Description-md5: 0d0bbaed314d7d26588d112ee4ede074

@ziotom78
Copy link
Contributor Author

Following your suggestion, I downloaded the most recent version of node (14.15.2) and the problem disappeared. Fantastic!

It seems that the globalThis symbol causing the error was introduced in Node 12 (https://blog.logrocket.com/what-is-globalthis-why-use-it/).

From my side, this issue can be closed. However, I expect that other Ubuntu/Mint users might stumble upon this problem.

@timotheecour
Copy link
Member

maybe this could be documented somehow

@ziotom78
Copy link
Contributor Author

maybe this could be documented somehow

Should I open a new issue, or is it better to reopen this?

@timotheecour
Copy link
Member

PR would be best

@ziotom78
Copy link
Contributor Author

Ok, but please give me an hint where to document this. My idea is to put the information in the page

https://nim-lang.org/docs/backends.html#backends-the-javascript-target

Would it be fine, or is there a better place where to place it?

@timotheecour
Copy link
Member

yes, there (backends.rst); maybe simply add a line in the appropriate place indicating if you have error such and such, make sure your node version is recent enough (e.g. >= ...)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants