2

An NPM module offer some extensions in a folder :

gojs
   extensions
       DebugInspector.js
   release
   packages.json

Each extensions contain one class without any (es2015) export syntax like :

function Inspector(...) {

}

Inspector.prototype.inspectObject = function(obj) {

}

How to use it with webpack ? I tried a simple export like :

import 'gojs/extensions/DebugInspector.js';

The file is correctly imported and compiled but I can't create a new instance of DebugInspector, the Inspector function is undefined. It's probably, surely a duplicate question but I can't find any example like this code (or I dont use the correct words to search).

This example is not related to the Mike's example because the imported file doesn't support any AMD mechanism.

Jerome
  • 603
  • 2
  • 5
  • 15
  • You really have to export something in order to import and use it elsewhere. I would suggest forking the repository and creating your own NPM module with an added export statement. Perhaps you can even get the original maintainer to merge it into their code base. – shotor Jul 03 '17 at 13:53
  • Humm it was my anxiety. And with a simple require ? Same sound ? – Jerome Jul 03 '17 at 14:06
  • Same I'm afraid. Modules don't actually exist yet in JavaScript. When you use JS modules today you're essentially wrapping your code in functions provided by your build system (node, webpack, requirejs etc.). These functions depend on the `exports` object being filled. If it's not filled, they happily continue and not expose anything. – shotor Jul 03 '17 at 14:55
  • Possible duplicate of [Importing "regular" javascript packages installed with npm in Webpack](https://stackoverflow.com/questions/39813594/importing-regular-javascript-packages-installed-with-npm-in-webpack) – Heretic Monkey Jul 03 '17 at 15:33
  • @MikeMcCaughan not the same thing because the package in the related example support AMD, not mine. – Jerome Jul 03 '17 at 15:46
  • Sure, but I get the feeling the answer will be the same. Perhaps you could try the advice presented there (adding `export ...` to the end of the file) before determining that the question is not a duplicate. – Heretic Monkey Jul 03 '17 at 16:29
  • 1
    @MikeMcCaughan, I can but I dont want the main principe of NPM is to maintain module to the latest version. I'm very suprise Webpack can't import a file as "global" like a simple "script" tag without any use of AMD/UMD/etc. or ES2015 mechanism. – Jerome Jul 04 '17 at 07:20
  • Possible duplicate of [How to include external file with webpack](https://stackoverflow.com/questions/28964886/how-to-include-external-file-with-webpack) – Liam Jan 05 '18 at 12:42

0 Answers0