72

How to include assets from external library into Angular CLI project

I am trying below but this does not work,

  "assets": [
    "../node_modules/<external library>/assets/"
  ]

Scripts are working fine though,

 "scripts": [  
    "../node_modules/<external library>/some.js",     
    "startup.js"
 ]

Angular Version : 2.4.1

Angular CLI : 1.0.0-beta.24

Any suggestion?

3
  • try to remove the last / after assets Commented Jan 9, 2017 at 20:55
  • Removed last /, Doesn't work Commented Jan 9, 2017 at 20:59
  • try "assets": [ "../node_modules/<external library>/assets/*" ] Commented Apr 23, 2019 at 8:53

4 Answers 4

124

This does now exist!

Fix #3555

To use it, update your .angular-cli.json file like so...

Angular version 2-5:

"assets": [
  "assets",
  { "glob": "**/*", "input": "../node_modules/<external library>/assets/", "output": "./assets/" }
]

Angular version >= 6:

"assets": [
  "src/favicon.ico",
  "src/assets",
  {
    "glob": "**/*",
    "input": "./node_modules/<your-node-module>/<possibly-subfolders>/",
    "output": "./assets/"
  },
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately this doesn't work any more for angular 6. For angular 6 config see my answer below.
I had to changed: "input": "../node_modules/<external_library>/..." to "input": "./node_modules/<external_library>/..." node_modules is as the same level at angular.json
What's the best way to have it work for "ng serve" too? I know you could add a symbolic link locally so it looks like the file is in the assets folder - is there another way?
In pre-Angular 6 this method should work for both "ng build" AND "ng serve". They both use .angular-cli.json.
And then how can we reference these assets in <img> or css rule like background-image: url(...). ?
69

Since angular 6 the config has changed slightly. To achieve this now, change the assets property of the respective builder in angular.json (beware, there are at least two relevant builders in the architects build and test!)

"assets": [
  "src/favicon.ico",
  "src/assets",
  {
    "glob": "**/*",
    "input": "./node_modules/<your-node-module>/<possibly-subfolders>",
    "output": "./assets/<possibly-subfolders>"
  },

3 Comments

Is there a way to only include specific files out of that folder?
Works with "glob": "**/*(de|us|es).svg"
1

Unfortunately, this doesn't exist yet :(. I'm desperately awaiting this feature also. Feel free to track this feature request here for Angular-Cli. Copying assets from node_modules

Updated

See @luvaas response as of Angular 6!

Comments

1

I am new here but I have this issue with npm package like in my case I have to call another application UI through npm package in current application and image from package itself.

2 changes we need this case :

  • Angular 6 above, in respective ng-package json add : assets:['./assets'] in child application as this will create assets/image folder in dist folder on build.

  • In angular.json add (in both architect and test) in parent application:

assets: [
   {
      "glob": "**/*",
      "input":"node_modules/"path of assets folder"/images"
   },
   "output":"./assets/images/"
]

1 Comment

do you know how we can reference these assets in <img> or css rule like background-image: url(...). ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.