I am creating an angular library (version 6) which is based on angular material for which I need to include the hammer js library.
I know that in angular 6 we can add the external js library in the angular.json
under the project's configuration. But this does not work in case of above library. I tried to add the external library in the following way.
"my-library": {
"root": "projects/my-library",
"sourceRoot": "projects/my-library/src",
"projectType": "library",
"architect": {
"build": {
"builder": "@angular-devkit/build-ng-packagr:build",
"options": {
"tsConfig": "projects/my-library/tsconfig.lib.json",
"project": "projects/my-library/ng-package.json",
"scripts": [
"../node_modules/hammerjs/hammer.min.js"
]
}
}
}
But I am getting this error.
Schema validation failed with the following errors: Data path "" should NOT have additional properties(scripts).
Please suggest what is the correct way to add external js file in the angular library.
you can add your external library in the tsconfig.lib.json file.
Open that file and add your desires library under types node that you can find in compilerOptios. I give you and example. In my case I referenced the library from my project node module. You can do the same with your hammer.js library. Good luck
You can't, that's the behavior of
ng-packagr
, you'll get that error every time you try to add assets to a Library. check this discussion for possible solution : https://github.com/angular/angular-cli/issues/11071Regarding the OP's specific example trying to add hammer.js. Just npm install hammerjs then edit your main.ts file and add
import 'hammerjs'
then it will be available globally.Also see this article.
You should try below command if you have used angular version 1.x before. And it works for me.
first add the libbrary in index.html or in
If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it:
typings.d.ts
file in yoursrc/
folder. This file will be automatically included as global type definition.Then, in
src/typings.d.ts
, add the following code:Finally, in the component or file that uses the library, add the following code:
please follow the link
remove
../
before path of node_modules