0

I am using an external javascript library in my angular 8 app containing a require statement at the first place.

require('dependency')

However I included the dependency and the other library in the angular.json scripts-section.

The problem is that the require-method does not get called or is not known:

someLibrary.js:1 Uncaught ReferenceError: require is not defined

So how can I solve this issue in context of angular.

Here are my compiler options too:

 "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "module": "commonjs", // <-- value: "es2015" also not solves the issue
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": ["node_modules/@types"],
    "lib": ["es2018", "dom"]
  }

Does anyone know how to fix this? does this has something to do with this here: https://stackoverflow.com/a/950146/4457758

ArgV
  • 175
  • 3
  • 13
  • Is this a custom module or a npm dependency? – Norbert Bartko Sep 12 '19 at 14:41
  • Yes I've installed both pure js-modules/libs the one which is required and the one which includes the require statement via npm install ... – ArgV Sep 12 '19 at 14:43
  • Import of javascript librairies with export of a function or class instead of modules or objects is the issue for some scenarios – Naga Sai A Sep 12 '19 at 14:51
  • @ArgV, refer this link for more details- https://stackoverflow.com/questions/37651495/difference-between-import-from-and-import-require-in-typescript – Naga Sai A Sep 12 '19 at 14:52
  • Are you calling `require('dependency')` from your TypeScript source code in your Angular app? – Reactgular Sep 12 '19 at 14:59

1 Answers1

0

I fixed this issue by just using browserify on the JS file I wanted to be loaded into the browser. Be sure to use the --s command so you can expose your js globally so that your angular app can interface with it.

For example, you might do something like:

browserify dependency.js --s myDependency
Eli Davis
  • 415
  • 5
  • 6