0

I am new to javascript and trying to use some generated code and running into some issues regarding exports. As file1 is auto-generated, if possible I would prefer to edit file2 only.

In file1.js I have:

var exports = function(apiClient) {
    ...
    this.apiClient = apiClient || ApiClient.instance;
    this.listAssets = function(opts, callback) {
    ...
    }
}

I want to use the function listAssets in file2.js. I tried doing the following in file2.js:

var myInstance = require('../../jsCode/src/api/file1');

and then calling myInstance.getAssets(), but that is clearly incorrect. I assume that I am missing something pretty obvious, but after looking around I still couldn't figure it out. Any direction would be appreciated.

A. K.
  • 150
  • 9
  • 2
    Check https://stackoverflow.com/a/5311377/7362396 - basically it says you may use just `exports`, but must not re-assign it like you do with `var`. Or just use the full `module.exports`. – Tobias K. Aug 24 '18 at 19:46
  • Read through this, so if I want to keep the var exports part (Would prefer to not change autogenerated code) where can I use the module.exports? – A. K. Aug 24 '18 at 19:52
  • Then you can do `module.exports = exports` at the end of `file1.js` – Tobias K. Aug 24 '18 at 19:59

2 Answers2

1

Use module.exports:

module.exports = function(apiClient) {
    ...
    this.apiClient = apiClient || ApiClient.instance;
    this.listAssets = function(opts, callback) {
    ...
    }

    // now return an object containing the API you need in your import
    return { 
        apiClient: this.apiClient, 
        listAssets: this.listAssets 
    };
}

and then require the module as you suggested:

var myInstance = require('../../jsCode/src/api/file1')(apiClient);

This is called CommonJS and is a set of agreements of how to handle module exports and imports. Remember your imported function needs to be passed apiClient as an argument.

connexo
  • 53,704
  • 14
  • 91
  • 128
  • Interesting - I assume that my problem is the passing apiClient. What does the (apiClient) after the require statement do? – A. K. Aug 24 '18 at 20:10
  • It executes the imported function with `apiClient` as an argument. – connexo Aug 24 '18 at 20:11
  • And if file1 already requires apiClient and uses it, in file2 when I am importing file1, do I need to instantiate a new apiClient instance, or will that be taken care of by file1? – A. K. Aug 24 '18 at 20:13
  • The way you access apiClient seems a little unusual. Can't you just require the apiClient in file1? Since your module in file1 exports its apiClient, you can use that in fileX where you import from file1. – connexo Aug 24 '18 at 20:14
  • Earlier in file1 is the line `module.exports = factory(require('../ApiClient')` – A. K. Aug 24 '18 at 20:15
  • You can only have one generic module.exports per file. – connexo Aug 24 '18 at 20:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/178712/discussion-between-a-k-and-connexo). – A. K. Aug 24 '18 at 20:18
-1

You can use the following solution:

module.exports = function()
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
user3697034
  • 127
  • 1
  • 1
  • 7
  • Where would I use this? In file2? – A. K. Aug 24 '18 at 19:53
  • module.exports = function(apiClient) { ... this.apiClient = apiClient || ApiClient.instance; this.listAssets = function(opts, callback) { ... } } – user3697034 Aug 24 '18 at 19:53
  • that change isnt ideal as I would prefer to not change file1 (its autogenerated code), but even with this fix I still cant call the methods defined in this function. Any way to do this by changing my procedure in file2? – A. K. Aug 24 '18 at 19:56