Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  195] [ 4]  / answers: 1 / hits: 36630  / 7 Years ago, sun, august 20, 2017, 12:00:00

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?


More From » node.js

 Answers
33

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


[#56708] Thursday, August 17, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arron

Total Points: 663
Total Questions: 119
Total Answers: 112

Location: Belize
Member since Mon, Jun 20, 2022
2 Years ago
;