0

I create a websitesite, where i want to include a javascript file using classes. But i do not know how to use it in a browser environment. The javascript file looks like this and can not be changed:

"use strict";
/* tslint:disable */
//----------------------
// <auto-generated>
//     Generated using the NSwag toolchain v12.0.15.0 (NJsonSchema v9.13.22.0 (Newtonsoft.Json v11.0.0.0)) (http://NSwag.org)
// </auto-generated>
//----------------------
// ReSharper disable InconsistentNaming
Object.defineProperty(exports, "__esModule", { value: true });
class Client {...}
exports.Client = Client;
class JComponent {...}
exports.JComponent = JComponent;
class Anonymous {...}
exports.Anonymous = Anonymous;
class SwaggerException extends Error {...}
exports.SwaggerException = SwaggerException;
function throwException(message, status, response, headers, result) {...}
//# sourceMappingURL=ProductCatalogClient.js.map

When i am trying to import it like this:

<script src="ProductCatalogClient.js"></script>

I become the error: Uncaught ReferenceError: exports is not defined at ProductCatalogClient.js:9

How do i include it correctly to have access to the Client class?

FleMo
  • 539
  • 6
  • 20
  • 2
    @alnitak nope ... (it's CommonJS) – Jonas Wilms May 28 '19 at 09:09
  • This looks as if it would be generated by some JS bundler (Webpack, Babel) are you able to change its configuration (and bundle your code into it...) ? – Jonas Wilms May 28 '19 at 09:10
  • oops - not a 'module' in the current meaning of the word, then! :p – Alnitak May 28 '19 at 09:10
  • @JonasWilms Yes it is generated from a swagger definition into a typescript file which is then transpiled to javascript. I can't change it directly and i don't know how it shoud be the best. – FleMo May 28 '19 at 09:12
  • Is this the full JS file or just a part? The `Object.defineProperty(exports, "__esModule", { value: true });` is part of a babel transformation alright, but in my file it's preceeded with `function(require,module,exports){` and other code, so the `exports` variable should exist. If this is the full JS file, the transpilation's failing somewhere and the problem can be in alot of different places. If this is not the full JS file, we need more information and or code. – Shilly May 28 '19 at 09:24
  • @Shilly I Included the whole js file except the bodys from the classes and the function. – FleMo May 28 '19 at 09:29
  • `// Object.defineProperty(exports, "__esModule", { value: true });` So that line is a comment instead of live code? Hmm, I do not know enough about Typescript to answer this. It just looks to me like there's faulty transpilation. Your JS file seems to include commonJS modules, but does not include the code to make commonJS work in the browser. Are you supposed to include a `require` library? Does typescript do this automatically? Is the swagger -> typescript -> commonJS -> browser chain set up correctly? – Shilly May 28 '19 at 09:35
  • @Shilly Sorry. It isn't a comment. I commented it for testing. – FleMo May 28 '19 at 09:41

0 Answers0