question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Global scripts are ignored

See original GitHub issue

Bug Report

What is the expected behavior?

The list of global scripts defined in angular.json should be compiled into the server code.

What is the current behavior?

The list of global scripts defined in angular.json are ignored.

What modules are related to this issue?

idk

Minimal reproduction with instructions:

To reproduce, clone the universal-starter repo.

In src/app/app.component.ts add this function declaration:

declare function globalFunction(): void;

And call it in the constructor:

export class AppComponent {
  constructor() { globalFunction(); }
}

Next, create the file src/javascripts.js and add this to it:

function globalFunction() {
  console.log("Hello, world!");
}

Finally, edit angular.json and add the new JavaScript file to the scripts array:

"scripts": [
  "src/javascripts.js"
]

Run ng serve and visit http://localhost:4200…you should see “Hello, world!” printed to the JavaScript console.

Now build the Universal app using npm run build:prerender…you should see the error: ReferenceError: globalFunction is not defined.

What is the use-case or motivation for changing an existing behavior?

It is common to include global scripts in the scripts attribute of angular.json. However, when running things out of Angular Universal, these scripts are ignored causing runtime errors.

Environment:

@nguniversal versions

  • aspnetcore-engine:
  • common:
  • express-engine:
  • hapi-engine:
  • module-map-ngfactory-loader:
Angular CLI: 7.1.4
Node: 8.10.0
OS: linux x64
Angular: 7.1.4
... animations, cli, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.11.4
@angular-devkit/build-angular     0.11.4
@angular-devkit/build-optimizer   0.11.4
@angular-devkit/build-webpack     0.11.4
@angular-devkit/core              7.1.4 (cli-only)
@angular-devkit/schematics        7.1.4
@ngtools/webpack                  7.1.4
@schematics/angular               7.1.4
@schematics/update                0.11.4
rxjs                              6.3.3
typescript                        3.1.6
webpack                           4.23.1

Is there anything else we should know?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
alan-agius4commented, Sep 26, 2022

You can use external libraries by importing (using ES6 imports) them instead of using scripts which will make them globally available in the browser.

0reactions
angular-automatic-lock-bot[bot]commented, Oct 27, 2022

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Are global scripts are ignored in Angular Universal?
It appears that Angular Universal ignores the list of global scripts ... No solution, just had to refactor code to not depend on...
Read more >
Is there a way to make a script be ignored by the Script ...
Hi everyone! Since I'm using some module scripts in my game which use lots of global variables and stuff like that, the script-analysis...
Read more >
Scripts ignore variables declared in 'module' scope - Ignition
Say I have the following code: # test.py def foo(): return 'foo1' + bar() def bar(): return 'bar1'. This works correctly in pure...
Read more >
Function ignoring global variables - The UNIX and Linux Forums
Hi there. I'm writing a function to which I want to pass a global variable. For some reason, it's ignoring the variable. pre ......
Read more >
Ignoring Code - ESLint - Pluggable JavaScript Linter
This message occurs because, normally, this file would be ignored by ESLint's implicit ignore rules (as mentioned above). A negated ignore rule in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found