javascriptangularnpmangular-clijs-beautify

Angular not able to import library


I'm installing js-beautify from https://www.npmjs.com/package/js-beautify

via npm install js-beautify --save

Then adding import to my app.component.ts

documentations says to

import jsbeautifier

But it needs quotes, so I'm using

import 'jsbeautifier'

but after that when I'm trying to use it I'm receiving error:

Failed to compile.

./src/app/app.component.ts
Module not found: Error: Can't resolve 'jsbeautifier' in '/home/oscar/IdeaProjects/lodashteatcherv4/src/frontend/src/app'
 @ ./src/app/app.component.ts 14:0-22
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi webpack-dev-server/client?http://localhost:4200 ./src/main.ts

I tried also this import

import '../../node_modules/js-beautify/js/lib/beautify.js'

I'm using angular cli 2/4


Solution

  • You are confusing Python instruction from documentation import jsbeautifier. In order to import this beautifier in javascript (client or nodejs) you need to do it this way:

    import { js_beautify } from 'js-beautify'
    

    However, I doubt you really want to do it this way, code beautification should probably not be part of your app.component.ts.