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.

Cannot use Rust because the NodeJS `util` module is not available

See original GitHub issue

I am trying to use Rust to program my screeps, and I am using wasm-bindgen for this (which is the standard way of combining Rust and JS).

However, this requires the TextDecoder API for encoding strings, which is available in the "util" module in NodeJS. But the "util" module is not available in screeps (require("util") gives an error).

In addition, TextDecoder and global.TextDecoder are not available either. Therefore I cannot use Rust with screeps.

The solution is for screeps to add in support for the "util" module in NodeJS, or alternatively upgrade to NodeJS version 11 (which has support for TextDecoder on the global object).

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
josephlrcommented, Sep 14, 2020

I filed a PR upstream to get this supported: https://screeps.com/forum/topic/3067/allow-access-to-node-s-textdecoder

Interestingly, TextDecoder already works in the simulator (as the object just exists at the global scope). I was able to get a Rust “Hello World” program working in the tutorial.

1reaction
josephlrcommented, Sep 14, 2020

I don’t know anything about wasm-bindgen, but I tried your code, and yep could reproduce this. Have you a way to import a plain javascript file ? If so, you could iimport a polyfill and target in no-modules, no ?

I think the best way forward is to just give screeps add the ability to call requires("util") in their Node installation. This API has been supported since Node 8, so this should just be a config change (as screeps mandates Node 10). The current Rust API bindings for screeps use an old version of stdweb that avoids TextDecoder IIRC. The two main issues are that stdweb is no longer maintained, and the replacement wasm-bindgen (which is offically supported by the Rust project) uses the TestEncoder/Decoder APIs to speed up calling across the js/wasm boundary.

I’m not super familiar with how screeps is maintained/updated, but I’d be happy to submit a PR if someone could give me some pointers about how to change things.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to import util module in Node.js? - Stack Overflow
I confirmed this method is available on command line: D:\>node Welcome to Node.js v12. · Here is my code: class App extends Component...
Read more >
How to Use Rust with Node.js When Performance Matters
Rust promises speed, safety, and concurrency. Let's see how can you use it with Node.js.
Read more >
Rust and Node.js: A match made in heaven - LogRocket Blog
Worker threads are great, but they're not available in all supported versions of Node. Fortunately, you can use Rust to build a native ......
Read more >
Web Extensions - Visual Studio Code
Web extensions still have access to the full VS Code API, but no longer to ... work because there is a shim for...
Read more >
How to solve "Error: Cannot find module '*.js'" with Node.js
then you are most likely trying to run the wrong file. It is possible you are missing a dependency that is needed from...
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