7

How can I group and export multiple functions in nodejs?

I am trying to group all my util functions in utils.js:

async function example1 () {
    return 'example 1'
}

async function example2 () {
    return 'example 2'
}

module.exports = { example1, example2 }

And then be imported in home.js:

  import { example1, example2 } from '../utils'

  router.get('/', async(ctx, next) => {
    console.log(example1()) // Promise { 'example 1' }

  })

I thought I would get 'example 1' for the test case above?

Any ideas?

Run
  • 54,938
  • 169
  • 450
  • 748
  • stupid question: did you forget to call the function? like: `console.log(await example1());`? – Roland Starke Aug 20 '17 at 10:25
  • 1
    You're getting `Promise { 'example 1' }` instead of `example 1` because you marked `example1` as async function (unclear) why. To get resolved value call it this way: `await example1()`. – alexmac Aug 20 '17 at 10:54

3 Answers3

18

This would be my solution for your exporting problem! And don't mix es5 exports with es6 imports, that can get very weird - sometimes!

export const example1 = async () => {
   return 'example 1'
}

export const example2 = async () => {
   return 'example 2'
}


// other file
import { example1, example2 } from '../../example'
return example1()

Nevertheless if you have to mix them, just let me know! We can find a solution for this aswell!


More about exporting modules and what can go wrong!

MDN Exports and the a short story about the state of javascript modules

marpme
  • 2,363
  • 1
  • 15
  • 24
  • thanks for the answer. how is `es5 exports` like? what is considered `es6 exports` – Run Aug 20 '17 at 10:32
  • 1
    ES5 export is the old syntax like `module.defaults = { example1, example2 }`. The newer (ES6) one is `export default { example1, example2 }` – marpme Aug 20 '17 at 10:33
  • thanks. what about this `export { example1, example2 }`? is it es5 as well? – Run Aug 20 '17 at 10:34
  • 1
    Everything you see in this MDN documentation is ES6 exports: https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/export – marpme Aug 20 '17 at 10:35
  • is this still es5, e.g. `module.exports = router;`? – Run Aug 20 '17 at 10:37
  • 1
    Yes this is the es5 export syntax. Here's also a nice article about exporting and weird things, that may happen: https://medium.com/webpack/the-state-of-javascript-modules-4636d1774358 – marpme Aug 20 '17 at 10:38
  • btw, i will get an error for `export default { example1, example2 }` - `TypeError: (0 , _utils.example1) is not a function` ` – Run Aug 20 '17 at 10:40
  • 2
    If you use the `export default` method, you have to import it like this: `import variable from '../file'`, but if you use `export const example1 = () => {}` then you have do the import like this `import { example1 } from '../file'`. – marpme Aug 20 '17 at 10:42
  • FYI you can also use the exact code you have as `export async function example1(){}`. Converting to arrows is a separate choice. – loganfsmyth Aug 20 '17 at 17:43
7

Below I have shared a way to declare the exporting functions in 2 different ways. Hope it helps understand the different ways in which it could be solved.

"use strict";
// utils.js

const ex1 = function() {
  console.log('ex1');
};

function ex2(context) {
  console.log('ex2');
};

module.exports = { example1: ex1, example2: ex2 };

You could invoke them in another (external) JS file (ex: app.js) as follows:

// app.js
const utils = require('./utils');

utils.example1(); // logs 'ex1'
utils.example2(); // logs 'ex2'
Hamzeen Hameem
  • 2,360
  • 1
  • 27
  • 28
  • should ```module.exports = { ex1, ex2 };``` work if I import ```const utils = require('./utils'); utils.ex1(); utils.ex2(); ``` – alete Aug 09 '19 at 21:35
1
async function example1 () {
    return 'example 1'
}

async function example2 () {
    return 'example 2'
}

module.exports.example1 = example1;
module.exports.example2 = example2;

import in home.js like this:

const fun = require('./utils');
fun.example1();