1

I am using Angular 8. This is the NPM library that I want to use : https://www.npmjs.com/package/convert-units

However, it is probably for Node.js, as it seems. Is there any way to make it work in my Angular component?

joler-botol
  • 442
  • 1
  • 7
  • 22
  • Similar to [this](https://stackoverflow.com/questions/51712942/how-to-use-node-js-module-with-angular-6) question – mkHun Jan 20 '20 at 09:42

3 Answers3

2

Yes, you definitely can use many of the NPM packages in the browser (Angular, React or any other framework/tech stack).

As for convert-units package. Just install it using npm install convert-units --save and then import using either:

const convert = require("convert-units"); // as according to documentation

or

import * as convert from "convert-units"; // to use new ES syntax

Example: Edit npm convert-units

Alexey Volkov
  • 978
  • 6
  • 4
0

As it is a utility package you can use it in node as well as angular. For angular you have to change the import to

import convert from 'convert-units';
0

First install the npm package as

npm install convert-units --save

Then in your component import as given below

import convert from 'convert-units';

Please find the working stackblitz: https://stackblitz.com/edit/hello-angular-6-u27n5j

Vikash B
  • 945
  • 12
  • 26
  • 1
    Thanks for the answer. I am getting this error. ERROR in src/app/app.component.ts(2,8): error TS1192: Module '"C:/Users/M1049043/Desktop/unit/unitt/node_modules/@types/convert-units/index"' has no default export. – joler-botol Jan 20 '20 at 09:51
  • Can you try to rebuild your application and test it. – Vikash B Jan 20 '20 at 09:55
  • I did. Getting this error - "resolves to a non-module entity and cannot be imported using this construct. " – joler-botol Jan 20 '20 at 09:56
  • Are you using "allowSyntheticDefaultImports": true in your tsconfig.json? If not, can you please add this and try – Vikash B Jan 20 '20 at 10:00
  • Yes, I am. Still, the "convert-units" part in the import statement is being marked red. – joler-botol Jan 20 '20 at 10:01