Gregology
Gregology

Reputation: 1725

is not a function in electron chrome but not node console

I'm using the npm package, foreach-batch in an electron project. I have the package installed and there is no Cannot find module error.

var forEachBatch = require('foreach-batch')
var stuff = [0,1,2,3,4,5,6,7,8,9]

forEachBatch(stuff, function(i) { console.log(i) }, 2, function(progress) {
  console.log(progress);
}, 1000);

The code runs as expected in the node console

$ node
> var forEachBatch = require('foreach-batch')
undefined
> var stuff = [0,1,2,3,4,5,6,7,8,9]
undefined
> 
> forEachBatch(stuff, function(i) { console.log(i) }, 2, function(progress) {
...   console.log(progress);
... }, 1000);
0
1
0.2
...

However, when I start up electron with a npm start and enter the same code in the chrome console. I receive an Uncaught TypeError: forEachBatch is not a function

Uncaught TypeError: forEachBatch is not a function

I'm new to Node and Electron, any insight that would help me understand the architecture better would be appreciated.

Upvotes: 0

Views: 128

Answers (1)

Molda
Molda

Reputation: 5704

The module does not return anything in a browser.

A bit of its code

var forEachBatch = function() { .... };
window.forEachBatch = forEachBatch;

So when you do this

var forEachBatch = require('foreach-batch')

then since the require function does not return anything you overwrite window.forEachBatch and it becomes undefined.

So try this instead

require('foreach-batch')
forEachBatch(...);

I haven't try it thou.

Upvotes: 1

Related Questions