0

I'd like to use the JavaScript package cesium-navigation (that installs via npm and node) in my project. However, my project uses webpack and TypeScript instead of just plain JavaScript. The package itself is not available in the npm repository, meaning it cannot be employed using npm install 'packageNameHere' --save; additionally, the use of javascript in that package means that no types exist for use in TypeScript.

How can I adapt this package to be used in a TypeScript, webmodules environment? Would I have to install this manually every time I wanted to use it?

jos
  • 149
  • 9
  • 1
    Have you tried installing the module directly from github? If I understand your question right, that should be enough – Marcin Wosinek May 12 '21 at 04:37
  • @MarcinWosinek How would that be done so that I can integrate it into the webpack project? I'm not sure of how it can be installed directly from github, given that it needs to tie into the webpack project (and be imported into typescript files in that manner too) – jos May 12 '21 at 04:44
  • @MarcinWosinek is probably referring to installing using `npm install --save git+https://github.com/alberto-acevedo/cesium-navigation.git` (as detailed [here](https://stackoverflow.com/a/17509764/3068190)). – samthecodingman May 12 '21 at 05:05
  • @samthecodingman I tried installing that way just now, but the installing process hung forever on `idealTree: timing arborist:ctor Completed in 1ms` right at the beginning. Never made it further than that – jos May 12 '21 at 06:00
  • Then just use the fork: [`@znemz/cesium-navigation`](https://www.npmjs.com/package/@znemz/cesium-navigation). – samthecodingman May 12 '21 at 06:13
  • @samthecodingman is there a way to install via `npm` instead of using `yarn`? – jos May 12 '21 at 06:29
  • 2
    Just because the README says `yarn` doesn't stop you from using `npm`, in most cases they are interchangeable. – samthecodingman May 12 '21 at 06:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232268/discussion-between-jos-and-samthecodingman). – jos May 12 '21 at 06:54
  • `Cannot find module '@znemz/cesium-navigation' or its corresponding type declarations.` – jos May 12 '21 at 07:18

1 Answers1

0

Add a file named types.d.ts and declare the module.

declare module "Cesium";

You can also declare all of the available options and types. You might need to restart typescript so the new types get picked up.

If you use Visual Studio Code - hover over the import statement and it will tell you what to put in your types.d.ts file.

Sean W
  • 5,663
  • 18
  • 31
  • `Cesium` is a different library unfortunately, `cesium-navigation` uses it to work. Where would I install the npm package such that I would be able to levarage the `types.d.ts`? Would I have to install it manually every time I wanted to use it? – jos May 12 '21 at 04:14
  • You add a file named "types.d.ts" in your source directory and add the above code - you might need to change the module name from "cesium" to what it's referred to in the library maybe "Cesium". You will have to add this regardless of how it's installed because there aren't type definitions in the 3rd party source. You could package the source yourself and create types, but it's simpler to just define the module. – Sean W May 12 '21 at 05:25
  • Apologies, just to clarify - I already have `cesium` installed through the npm repository and webpack, I want to use `cesium-navigation` as well which is not available via the npm repo. I imagine that your steps are relevant to `cesium-navigation` as well? – jos May 12 '21 at 05:35
  • Yes, in general, this is how you can use a JS library without types in a TS environment without a bunch of TS errors. It doesn't matter how the library was installed. You might be able to install @types/cesium for cesium and then define a module for cesium-navigation like above to make your life a little easier. – Sean W May 12 '21 at 07:29