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.

Buffer is undefined in electron

See original GitHub issue

If I compile a .jsc file inside an electron process with bytenode.compileFile(‘./test.js’, ‘./test.jsc’); and that file contains references to node-js’s Buffer object. It is undefined.

So a “new Buffer(1);” gives a TypeError: undefined is not a constructor exception. And this exception only occurs when requiring the .jsc file of course. The .js file works fine.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12

github_iconTop GitHub Comments

2reactions
haakonnessjoencommented, Feb 25, 2019

Example file test.js: var buf = new Buffer(1);

inside your main.js in electron:

v8.setFlagsFromString('--no-lazy');
bytenode.compileFile('./test.js', './test.jsc');
require('./test.jsc');

Result:

TypeError: undefined is not a constructor
    at Object.<anonymous> (evalmachine.<anonymous>:1:167)
    at Object.<anonymous> (evalmachine.<anonymous>:1:185)```
0reactions
OsamaAbbascommented, Aug 27, 2019

I don’t understand why you had to incorporate webpack and all its spells in your example 😃 . I’m trying to get the app logic to isolate the bytenode aspect of the issue.

However, if you were able to solve the issue and get the app run by adding Buffer = require("buffer").Buffer, then you are correct: it’s the same issue discussed here. (and it can be demonstrated easily by using Buffer object in a very minimal electron setup). So, solving this problem requires that we study the source code of electron itself, to see where exactly the deletion of Buffer object occurs, and why this happens in the case of .jsc files alone.

I tried this approach before but did not spend much time on it. I’ll look into it again and get back to you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Buffer is undefined in electron · Issue #10 · bytenode ... - GitHub
If I compile a .jsc file inside an electron process with bytenode.compileFile('./test.js', './test.jsc'); and that file contains references ...
Read more >
Uncaught ReferenceError: Buffer is not defined - Stack Overflow
Answering my own question. Two things helped to resolve the issue: Adding plugins section with ProviderPlugin into webpack.config.js.
Read more >
How i can solve Electron error: "Buffer is not defined"?-ember.js
Since Buffer isn't available on the web, you need to polyfill it. And since modern ember uses webpack, polyfilling Buffer is well documented....
Read more >
Electron target version - Haxe - OpenFL Community
Hi Is there any way to bump the Electron target's version and still successfully compile and run an OpenFL app? ... Buffer is...
Read more >
Buffer not defined in Webworker - Theia Community
The problem is that “Buffer” is not defined in the Worker that runs the plugins (see worker-main. ts).
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