you can add the script reference to angular.json like this as example include jquery library

"scripts": [
   {
    "input": "node_modules/jquery/dist/jquery.min.js",
    "inject": false,
    "bundleName": "jquery"
   }
]

then set inject to false will not injected the script to index.htm, finaly we just set a name for bundleName this way the script will not bundle with other script file and will be standalone file , and now you can add the script tag to the header tag

update the index.html and include the script tag at the header

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <title>Portfolio</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <script src="jquery.js"></script> <!--  -->
</head>

<body>
  <app-root></app-root>
</body>

</html>
Answer from Muhammed Albarmavi on Stack Overflow
🌐
DEV Community
dev.to › danywalls › working-with-3rd-party-script-and-css-in-angular-l88
Working With 3rd-Party Script and CSS In Angular - DEV Community
October 17, 2022 - What happens if you have more scripts like tracking stuff? What happens to keep the problem isolated in his bundle? The Angular.json solve all of them, providing a single place for the maintenance of CSS and Javascript files, with the option of eager loading, bundling, and optional injection in the index.html
Discussions

Scripts not loading from angular.json file
Versions Angular CLI: 6.0.3 Node: 8.9.4 OS: win32 x64 Angular: ... Package Version ------------------------------------------------------ @angular-devkit/architect 0.6.3 @angular-devkit/core 0.6.3 ... More on github.com
🌐 github.com
33
May 28, 2018
script file is not working by adding on angular.json file inside script array
When we are going to refrence any script file from angular project folder or from node_modules package folder it is not working. Ex: Suppose we are going to add bootstrap package to the angular pro... More on github.com
🌐 github.com
19
September 28, 2018
Angular Cli Webpack, How to add or bundle external js files?
Hi im using angular 7, when i run ... page, when i click in other page the script is unloaded, how can i load it on a specific page or for a whole application? 2020-11-05T19:59:41.993Z+00:00 ... You need to open file .angular-cli.json file and need to search for "scripts:" or ... More on stackoverflow.com
🌐 stackoverflow.com
Should scripts be put into angular.json or into index.html? - Stack Overflow
With Angular you have two options to load scripts (js and css): In the index.html: In the angular.json: " More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
medium.com › @sehban.alam › lets-understand-angular-json-deeply-in-your-angular-projects-2e156542940f
Let’s Understand angular.json Deeply in Your Angular Projects | by Sehban Alam | Medium
November 14, 2024 - In this blog, I’ll walk you through every corner of angular.json using Angular , breaking it down step by step, with code examples, best practices, and even some bonus tips to ensure you’re using it like a pro.
🌐
Angular
angular.dev › reference › configs › workspace-config
Workspace configuration • Angular
The angular.json file at the root level of an Angular workspace provides workspace-wide and project-specific configuration defaults. These are used for build and development tools provided by the Angular CLI.
🌐
GitHub
github.com › angular › angular-cli › issues › 11044
Scripts not loading from angular.json file · Issue #11044 · angular/angular-cli
May 28, 2018 - Step 3: I added all the necessary .css and .js files in the angular.json file including first script reference of jquery from nod_modules
Published   May 28, 2018
Author   tjpatange
🌐
GitHub
github.com › stackblitz › core › issues › 700
script file is not working by adding on angular.json file inside script array · Issue #700 · stackblitz/core
September 28, 2018 - But script files are not working. Files are added as the bellow formating: Inside angular.json file: "styles": [ "src/styles.css", "node_modules/bootstrap/dist/css/bootstap.min.css" ], "scripts": [ "node_modules/jquery/dist/jquery.min.js", "node_modules/popper.js/dist/umd/popper.min.js", "node_modules/bootstrap/dist/js/bootstap.min.js" ] According the the local system It should to work but script file is not working.
Author   altaf596
Top answer
1 of 5
97

Last tested using angular-cli 11.x.x with Angular 11.x.x

This can be accomplished using scripts:[] in angular.json.

{
  "project": {
    "version": "1.0.0",
    "name": "my-project"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": ["assets"],
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "polyfills.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": false,
      "styles": [
        "styles.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false
  }
}

Note: As the documentation suggests in the global library installation: if you change the value of your styles (or scripts!) property, then:

Restart ng serve if you're running it,

..to see the scripts executed in a **globalcontext via the scripts.bundle.js file.

Note: As discussed in the comments below. JS libs that support UMD modules via es6 imports such as jQuery can also be imported into your typescript files using the es6 import syntax. For example: import $ from 'jquery';.

2 of 5
25

There is a subtle difference to using scripts:[] then to adding something to the <head> with <script>. Scripts from scripts:[] get added to the scripts.bundle.js that gets always loaded in the body tag and will thus be loaded AFTER scripts in <head>. Thus if script loading order matters (i.e. you need to load a global polyfill), then your only option is to manually copy scripts to a folder (e.g. with a npm script) and add this folder as an asset to .angular-cli.json.

So if you really depend on something being loaded before angular itself (Option A), then you need to copy it manually to a folder that will be included in the angular build and then you can load it manually with a <script> in <head>.

Thus, for achieving option a you have to:

  • create a vendor folder in src/
  • add this folder as an asset to .angular-cli.json:
"assets": [
    "assets",
    "favicon.ico",
     "vendor"
  ]
  • copy your vendor script node_modules/some_package/somejs.js to vendor

  • load it manually in index.html: <head> <script src="vendor/some_package/somejs.js"> </head>

However most of the time you only need this approach for packages, that need to be available globally, before everything else (i.e. certain polyfills). Kris' answer holds true for Option B and you get the benefit of the webpack build (Minification, Hashes, ...).

However if your scripts need not be globally available and if they are module-ready you can import them in src/polyfills.ts or even better import them only when you need them in your specific components.

Making scripts globally available via scripts:[] or via manually loading them brings it own set of problems and should really only be used, when it is absolutely necessary.

Find elsewhere
🌐
Medium
medium.com › @clintpitzak › how-to-use-external-javascript-libraries-in-angular-8-247baacbdccf
How to Use External JavaScript Libraries in Angular 8 | by Clint Pitzak | Medium
June 19, 2019 - In your Angular project open the angular.json file locate the “assets”, “styles”, and “scripts” section.
🌐
Danywalls
danywalls.com › integrating-third-party-scripts-and-css-into-your-angular-app
Integrating Third-Party Scripts and CSS into Your Angular App | DanyWalls
October 15, 2022 - What happens if you have more scripts like tracking stuff? What happens to keep the problem isolated in his bundle? The Angular.json solves all of them, providing a single place for the maintenance of CSS and JavaScript files, with the option of eager loading, bundling, and optional injection in the index.html
🌐
YouTube
youtube.com › watch
Angular Json File – My Top 8 Settings (2022) - YouTube
The angular.json file is where we usually configure the angular workspace and every particular project or library. In this video, I will share my TOP 8 the m...
Published   July 19, 2022
🌐
GitHub
github.com › angular › angular-cli › issues › 17125
Allow .ts files in angular.json `scripts` to be transpiled to target ES version · Issue #17125 · angular/angular-cli
February 29, 2020 - "scripts": [ { "input": "src/firebase-messaging-sw.ts", "inject": false, "bundleName": "firebase-messaging-sw" } ] This solution suggests to transpile the .ts file before the Angular build (tsc my-file.ts && ng build).
Author   denkan
🌐
Reddit
reddit.com › r/angular › adding external javascript file to angular app
r/angular on Reddit: Adding external javascript file to Angular app
April 3, 2024 -

I am an Angular newbie, sorry if this is a very basic question. Unfortunately I could not find any help on the web.

This is my Windows 10 pc configuration: Angular CLI: 17.2.3 / Node: 20.11.1

I am trying to integrate an audio player into my Angular app. No problem with the css file, I have managed to merge it to sytles.css. The problem is with the player's js file. How do I add it to my app? Please be as simple as you can, a step-by-step process would be much appreciated.

Thanks for your help.

🌐
GitHub
github.com › angular › angular-cli › issues › 24592
Add script config for adding scripts as type="module" · Issue #24592 · angular/angular-cli
January 23, 2023 - "scripts": [ { "input": "node_modules/third-party/esm-script.js", "inject": true, "scriptType": "module" } ], ... area: @angular-devkit/build-angulardevkit/build-angular:browserfeatureIssue that requests a new featureIssue that requests a new featurefeature: under considerationFeature request for which voting has completed and the request is now under considerationFeature request for which voting has completed and the request is now under consideration
Author   tmcconechy
🌐
GitHub
github.com › angular › angular-cli › issues › 24893
angular.json scripts support .mjs file format · Issue #24893 · angular/angular-cli
March 22, 2023 - Which @angular/* package(s) are relevant/related to the feature request? No response Description Currently angular.json's scripts section does not support .mjs. Some front end packages such as mermaid 10 has moved to ES modules with .mjs...
Author   turingcs2005
🌐
CodeStack
codestack.net › angular › getting-started › package
Angular package.json file overview
You can clean existing and add your own new scripts. The list of packages installed as dependencies for this project are required at runtime. "dependencies": { "@angular/animations": "~8.0.1", "@angular/common": "~8.0.1", "@angular/compiler": "~8.0.1", "@angular/core": "~8.0.1", "@angular/forms": "~8.0.1", "@angular/platform-browser": "~8.0.1", "@angular/platform-browser-dynamic": "~8.0.1", "@angular/router": "~8.0.1", "rxjs": "~6.4.0", "tslib": "^1.9.0", "zone.js": "~0.9.1" },
🌐
Reddit
reddit.com › r/angular2 › angular project: integrating external javascript and jquery scripts for enhanced functionality
r/Angular2 on Reddit: Angular Project: Integrating External JavaScript and jQuery Scripts for Enhanced Functionality
January 29, 2024 -

Hello Angular community,

I'm currently working on an Angular project where I have an existing HTML, CSS layout, and a JavaScript file that handles various click events, such as toggling a hamburger menu and more. My goal is to seamlessly integrate this JavaScript file into my Angular project without the need to convert each behavior to fit Angular's structure.

Here's what I've done so far:

  1. I created a file called ` script.js ` and moved all the JavaScript code into it.

  2. I also created separate files for jQuery and included the necessary scripts. Note that these files are required as the `script.js` file utilizes jQuery.

  3. I added references to these files in the ` angular.json` file, under the ` scripts ` key of the ` build` section.

  • I'm facing issues as the provided scripts are not running. Even a simple script like ` alert('js')` doesn't seem to work. I've noticed that the script is injected into the page when I inspect it using the developer tools, but it's not executing.

Thank you in advance for your help!

🌐
GitHub
github.com › Ismaestro › angular-example-app › blob › master › angular.json
angular-example-app/angular.json at master · Ismaestro/angular-example-app
"scripts": [] } }, "lint": { "builder": "@angular-eslint/builder:lint", "options": { "lintFilePatterns": ["src/**/*.ts", "src/**/*.html"] } } }, "i18n": { "sourceLocale": { "code": "en", "baseHref": "/" }, "locales": { "es": { "translation": "src/locale/messages.es.xlf", "baseHref": "/es/" } } } } }, "cli": { "analytics": "49e1320d-9031-4d19-bb7c-2d84a0f2fd49", "schematicCollections": ["@angular-eslint/schematics"] } }
Author   Ismaestro