2

I'd trying to add seedrandom to an angular project. I have installed it and typescript definitions using npm. My package json has the relevant entries (below) so im confident it installed ok, and the relevant folders are in node-modules.

"dependencies": {
  "@types/seedrandom": "^2.4.28",
  "seedrandom": "^3.0.5"
}

Despite this, I persistently get the error: "Property 'seedrandom' does not exist on type 'Math'." In fact, when I do ng serve the error throws in the console, but the web app works fine. When I do ng build I get the error and the build fails.

I've tried defining an interface per this but doesn't help. And I've stared at the code for two days too, that didn't help either.

  getRandomNumber(n) {

    const randomNumber = new Math.seedrandom( '1234' );
    return Math.round( Math.round( randomNumber() * n) );

 }

UPDATE: Following the answer from Myk I tried the following:

import { seedrandom } from 'seedrandom';

getRandomNumber(n) {
  const randomNumber = seedrandom( '1234' );
  return Mathround( Math.round( randomNumber() * n) );
}

only get error: Module '"../blah/node_modules/@types/seedrandom"' has no exported member 'seedrandom'.

Which is interesting because when I checked the @types/seedrandom/index.d.ts file is include the lines:

export = seedrandom;
export as namespace seedrandom;

How do I get seedrandom working in Angular??

Kissenger
  • 345
  • 4
  • 15
  • What are you trying to accomplish? – Fernando Zamperin Oct 01 '19 at 23:17
  • To implement the 3rd party library 'seedrandom' in an angular app. – Kissenger Oct 02 '19 at 19:01
  • Instead of `import { seedrandom }...`, try `const seedrandom = require('seedrandom')` as in https://stackoverflow.com/a/58193548/925478. Also, I'm not sure, but the typescript types module (version '2.4.28') seems to be for an older version of that package. – Myk Willis Oct 02 '19 at 19:38
  • `ERROR in src/app/tools.service.ts(17,24): error TS2304: Cannot find name 'require'.` – Kissenger Oct 02 '19 at 19:41
  • I dont know how to check the whether types are appropriate, but they were installed via `npm i @types/seedrandom`, so assume they are correct? – Kissenger Oct 02 '19 at 19:43
  • 1
    I'm not sure what to tell you. I've been trying to get it to work in this [stackblitz](https://stackblitz.com/edit/angular-kbrvsu?file=src%2Fapp%2Fapp.component.ts) but it keeps importing the function as a string instead of the full function. I think I'm missing something that someone else could help out with. – rhavelka Oct 02 '19 at 20:02
  • actually your example works for me when i change the line `this.test = seedrandom('5')` to `this.test = seedrandom('5')()`. But it still doesnt work in my project...On the import line I am still getting the no exported member error. – Kissenger Oct 02 '19 at 20:40

3 Answers3

2

According to the documentation:

Starting in version 3, when using via require('seedrandom'), the global Math.seedrandom is no longer available.

So it seems that if you wish to use it in your Angular application, installed via npm, you should import the function directly and use the random number generator it returns:

const seedrandom = require('seedrandom');
const rng = seedrandom('<seed>');
Myk Willis
  • 12,306
  • 4
  • 45
  • 62
2

import * as seedrandom from "seedrandom"; followed by console.log(seedrandom('hello.')()); seems to work for me.

esModuleInterop seems to have something to do with. I just copy pasted the first import suggested there and didn't bother reading any further, so I don't know any details.

Thanks to your preliminary "staring at the code for two days" I didn't have to do that myself and was able to find a solution that works for me in under 10 minutes. Thanks for doing the hard work :-)

dave
  • 4,024
  • 2
  • 18
  • 34
0

Just for posterity, I couldn't solve this and ended up using one of the suggestions here as I dont need anything of super high quality

Kissenger
  • 345
  • 4
  • 15