Using exports in javascript

Using exports in javascript



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) 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:


listAssets


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.


myInstance.getAssets()





Check 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 at 19:46



exports


var


module.exports





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 at 19:52





Then you can do module.exports = exports at the end of file1.js
– Tobias K.
Aug 24 at 19:59



module.exports = exports


file1.js




2 Answers
2



Use module.exports:


module.exports


module.exports = function(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.


apiClient





Interesting - I assume that my problem is the passing apiClient. What does the (apiClient) after the require statement do?
– A. K.
Aug 24 at 20:10





It executes the imported function with apiClient as an argument.
– connexo
Aug 24 at 20:11


apiClient





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 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 at 20:14






Earlier in file1 is the line module.exports = factory(require('../ApiClient')
– A. K.
Aug 24 at 20:15


module.exports = factory(require('../ApiClient')



You can use the following solution:


module.exports = function()





Where would I use this? In file2?
– A. K.
Aug 24 at 19:53





module.exports = function(apiClient)
– user3697034
Aug 24 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 at 19:56






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế