0

I am trying to use a nodejs library like uuid in my typescript app. This simple import works just fine if I want to only use it in a specific class:

import { v4 } from "uuid";

class MyClass {
...
}

However, this makes the class MyClass not "discoverable" by any other files in my solution. Sure I could export it, but that forces me to import the class in every usage, and the problem spreads like a cancer to every file. Do I really have to import/export every single class/file in my application just because I want to produce a simple UUID?

I saw that I can use require instead, however typescript doesn't know what that keyword is. I found this question but neither installing @types/node nor the quick and dirty declare var require any works.

It really seems like I am jumping through a lot of unnecessary hoops just to generate a uuid in typescript. Am I doing something very wrong?

Thanks

Sillydan
  • 104
  • 13
  • What do you mean by "discoverable"? Even if you didn't import the library, you would still have to export the class and import it where you want to use it, because every file is a module in js/ts. – robertgr991 Apr 11 '21 at 11:19
  • I've been including them in my `index.html` with ` – Sillydan Apr 11 '21 at 11:27
  • 1
    You should build your source files and include the resulted bundle file in `index.html`, for example: ``. This will take care of the imports, but you still have to export/import, don't include them in a specific order and use the classes globally. Here are some link that can help you: [tooling](https://www.typescriptlang.org/docs/handbook/typescript-tooling-in-5-minutes.html), [migrating-from-js](https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html), [compiler-options](https://www.typescriptlang.org/docs/handbook/compiler-options.html). – robertgr991 Apr 11 '21 at 11:51

0 Answers0