Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including typings for third-party library gives module not found via angular CLI?

I am attempting to include typings for Apple's MapKit JS in my Angular 9 application, because there are no high quality typings provided by the library, nor any good third-party typings in the @types scoped package. However, Angular CLI isn't happy with how I've included my typings. The exact error I receive on compile is:

Module not found: Error: Can't resolve 'mapkit' in `components/map.component.ts`.

What am I doing wrong?

Typings

~src/types/mapkit/index.d.ts

This is where the typings are declared, and how they are declared.

declare module 'mapkit' {
  // interfaces, classes, etc here
}

Typescript Configuration

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2019",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

tsconfig.app.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "types": []
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ],
  "exclude": [
    "src/test.ts",
    "src/**/*.spec.ts"
  ]
}

Usage in code

Here is how mapkit is used.

import * as mapkit from 'mapkit';
like image 356
marked-down Avatar asked Jun 21 '20 04:06

marked-down


People also ask

Which file contains a reference to other third party installation in angular?

The node modules folder is the default folder where all 3rd-party libraries are installed, according to which our application runs. This is used for development purposes. When we run or build our project then only required libraries are put together in a bundle and then deploy into the server.

What is Typings in angular?

Typings describes contract of libraries you use. This allows the TypeScript compiler that what you use exist (classes, properties, ...). You can install typings from a repository using the typings command or let the compiler find out them leveraging the strategy specified in the tsconfig.

How to install and use third-party libraries in angular CLI project?

Today, we're going to figure out what are the ways to install and use third-party libraries in an Angular CLI project. Using ES6 Modules. The easiest way to add third-party libraries is to use ES6 Modules. A library, which is exported declaratively, could be loaded in a few straightforward steps.

Is the content of the angular 2+ CLI still applicable?

The content is likely still applicable for all Angular 2 + versions. The Angular CLI is a command line interface tool that allows us to quickly build and run our Angular applications. The CLI can help us quickly scaffold components and services in our app while following the best practices out of the box.

Is it possible to bring in third party modules to NPM?

One of the snag points I ran into was bringing in third party modules. The vast majority of common npm modules already have high quality typings files which you can manage through typings, but inevitably you'll run into that necessary little package that doesn't have the typings you need. Our project needed one such package, googleapis.

How do I add third part ES2015 modules to the CLI?

The CLI currently uses Webpack under the hood to handle module bundling in our apps. Webpack is a great open source tool that allows adding third part ES2015 modules easily to JavaScript applications. So lets start with adding a popular library, Lodash.


Video Answer


1 Answers

Long story short: You need to rename your folder with types and add them to the type roots.

Step #1

In your folder with types (./types) create new folder "apple-mapkit-js"

Step #2

Add your custom index.d.ts there

Step #3 Point TSC to your custom types. Update tsconfig.app.json with new typeRoots:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "outDir": "./out-tsc/app",
    "typeRoots" : ["./src/types/", "node_modules/@types"]
  },
  "files": [
    "src/main.ts",
    "src/polyfills.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ],
}

Step #4 Update your import to the

import * as mapkit from 'apple-mapkit-js'

Now it works.

But what was the issue?

Actually, according to the TypeScript documentation, if you specify typeroots, all other resources become "invisible" for the typescript, so tsc just didn't see your declarations

P.S. Checked on the Angular 9 app.

like image 197
Drag13 Avatar answered Oct 17 '22 03:10

Drag13