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

.range is not a function #103

Closed
jefffriesen opened this issue May 16, 2017 · 5 comments
Closed

.range is not a function #103

jefffriesen opened this issue May 16, 2017 · 5 comments
Labels

Comments

@jefffriesen
Copy link

I've got an example repo where I'm trying to get some simple examples working. I'm using webpack in a node environment. I would like to include lodash functions in some of the parallel processing but right now I'm just trying to get the simplest example working.

Here is the function:
https://github.com/jefffriesen/parallel-es-node-example/blob/master/src/parallel-range.ts

Here is the error:

TypeError: __WEBPACK_IMPORTED_MODULE_0_parallel_es__.range is not a function
    at Object.<anonymous> (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:86:51)
    at __webpack_require__ (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:20:30)
    at /Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:66:18
    at Object.<anonymous> (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:69:10)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)

parallel is importing something. I can see it here:
screen shot 2017-05-15 at 9 26 38 pm

Any suggestions?

@MichaReiser
Copy link
Owner

You need to use

import parallel from "parallel-es"

You find an example here using commonjs.

I had to release a new version as it seemed that webpack did not export the default export for some reason. This seems to be fixed with a newer webpack version. So just install the latest version

and you find the api documentation here

Besides, the webpack plugin does not yet work with node.js. So you can use webpack but you don't have to for parallel.es (but you can not use any variables from the outer scope).

@jefffriesen
Copy link
Author

I forgot to mention that I tried import parallel from "parallel-es" already. That was my first attempt. I've had a problem in the past where typescript wanted the * as packagename syntax so that was my last attempt.

I updated to 0.1.18 that you released a few hours ago and range is defined now (along with from, run and times) but now I get this error:

module.js:470 Uncaught Error: Cannot find module './node-slave.parallel'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.resolve (internal/module.js:27:19)
    at NodeWorkerThreadFactory.getSlaveFileName (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:34363)
    at NodeWorkerThreadFactory.value (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:34039)
    at t.value (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:31868)
    at t.value (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:31657)
    at /Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:41100
    at Array.map (native)
    at e.value (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:41056)
    at t.value (/Users/jeffers/git/jefffriesen/parallel-es-node-example/dist/parallel-range.js:85:37653)

But it sounds like the webpack compiling isn't suppose to work yet with node.js. Here is a nice summary of the difference between node.js webpack config and a browser config: http://stackoverflow.com/a/43202748/1884101

What needs to happen to get it to work with node.js?

Also, in node.js, does that mean I can't pass external functions to the function I want to run in parallel? This section in the docs I'm not clear on: https://github.com/MichaReiser/parallel.es#referenced-functions-variables-and-imports. Can I manually do what webpack does? I want to pass in a few lodash functions to the function I want to parallelize. Could I just pass the functions in as arguments?

Thanks

@MichaReiser
Copy link
Owner

Using it with webpack is a little bit more complicated for the moment. Just require your module. There is no need to use webpack in a node project. If you intend to use webpack, you need to copy the node-slave.parallel file from node_modules/parallel-es/dist into your dist folder, e.g. by using the copy plugin of WebPack.

However, I suggest you for the moment to just use plain require (or import with babel or TypeScript and compiling down to commonjs style).

Also, in node.js, does that mean I can't pass external functions to the function I want to run in parallel? This section in the docs I'm not clear on:

No, you can't at the moment. Passing in the functions will not be sufficient as lodash makes heavily use of closures that are, and cannot, be supported. There needs to be a transpilation step that includes the used functions. But I never got time to work on supporting node properly.

@jefffriesen
Copy link
Author

@MichaReiser Ok, maybe I made it more complicated than I needed to. Just require functions. That's fine. I ran into other problems that are more appropriate for a different ticket. Thank you.

jefffriesen added a commit to jefffriesen/parallel-es-node-example that referenced this issue May 17, 2017
…llel into distilled folder

Based on this thread:
MichaReiser/parallel.es#103 (comment)
5103
(By the way, I intentionally committing the /dist/ folder so people can
see how it works)
@jefffriesen
Copy link
Author

jefffriesen commented May 17, 2017

For reference for other people, here is a node.js repo that uses typescript compilation and webpack.

This works:

import parallel from "parallel-es"

parallel.range(0, 10)
  .map(value => value * value)
  .subscribe((subresult, taskIndex) => console.log(`The result of the task ${taskIndex} is`, subresult))
  .then(result => console.log(result))

// output:
// The result of the task 6 is [ 36 ]
// The result of the task 8 is [ 64 ]
// The result of the task 3 is [ 9 ]
// The result of the task 9 is [ 81 ]
// The result of the task 7 is [ 49 ]
// The result of the task 1 is [ 1 ]
// The result of the task 4 is [ 16 ]
// The result of the task 0 is [ 0 ]
// The result of the task 5 is [ 25 ]
// The result of the task 2 is [ 4 ]
// [ 0, 1, 4, 9, 16, 25, 36, 49, 64, 81 ]

Here I'm copying the node-slave.parallel.js file into the /dist/ directory: https://github.com/jefffriesen/parallel-es-node-example/blob/master/webpack.config.js#L32-L34

  plugins: [
    new CopyWebpackPlugin([
      {from: './node_modules/parallel-es/dist/node-slave.parallel.js'},
    ]),
  ],

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

No branches or pull requests

2 participants