mx_code
mx_code

Reputation: 2527

How to import npm packages in angular?

Im trying to import a package called cssdom in angular:

like this:

import * as CssDom from "cssdom";

But I'm getting the following error:

enter image description here

when I try to create a new instance of CssDom like this:

const css = '.container { background-color: white; }';
const cssdom = new CssDom(css)

I tried this way also:

import {CssDom} from 'cssdom'

Or is there a way to import these third party packages in to angular?

In vue-js this is very easy. As you can import simply. But with typescript/angular this seems hard.!

Upvotes: 0

Views: 755

Answers (1)

Melchia
Melchia

Reputation: 24314

First install cssdom (if not done yet):

$ npm i cssdom

Solution 1 :

Add the script to angular.json

  "scripts": [
     ...
    "../node_modules/cssdom/cssdom.js"
     ...
],

Next use cssdom like this:

declare const CssDom:any;

...

const css = '.container { background-color: white; }';
const cssdom = new CssDom(css)

Solution 2 :

declare var CssDom:any;

import "cssdom";

Upvotes: 1

Related Questions