1

I would like to use some 3rd party libraries in my Angular project, but since version 6 of Angular I get the reference error: global not defined. I've installed the library and added the @types library too. Unfortunately I didn't get it to work yet.

Is there anyone who can explain to me how to use 3rd party libraries in angular 6 and higher? I don't want to revert back to Angular 5 just for a javascript library.

1
  • 1
    which 3rd party library are you using? Commented Oct 19, 2018 at 12:47

2 Answers 2

1

add this to your index.html

<script>
  if (global === undefined) {
    var global = window;
  }
</script>

from: https://github.com/aws-amplify/amplify-js/issues/678

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the reply! I've seen this solution come up on various threads but adding this script raises a TypeError: superCtor is undefined. I also tried to add (window as any).global = window; to the polyfill.ts but it also raises the TypeError.
That's unfortunate. Have you find a solution yet? I'm planning to migrate a project from Angular 4 to 6 and knowing that issues (specially ones involving 3rd party libs) may arise I'm hesitant to do so
1

There are several ways to use it depends on what you need exactly.

Generally speaking for those legacy un-typed js library you can always use something like the following:

import * as jquery from 'jquery'

For libraries that require global access, you might need to tweak angular.json file and add reference to scripts field:

"scripts": [
    "node_modules/zoomooz/jquery.zoomooz.min.js"
],

For some tricky library with default export statements, you might even need to do:

import LIBRARYNAME from 'library-name' (without brackets)

So it really depends on what you need.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.