如何在 nodejs 中分组和导出多个函数?
我正在尝试将我所有的 util 函数分组到 utils.js 中:
async function example1 () {
return 'example 1'
}
async function example2 () {
return 'example 2'
}
module.exports = { example1, example2 }
然后在home.js中导入:
import { example1, example2 } from '../utils'
router.get('/', async(ctx, next) => {
console.log(example1()) // Promise { 'example 1' }
})
我以为我会得到上面测试用例的'example 1'
?
有什么想法吗?
最佳答案
这将是我解决您的导出问题的方法!并且不要将 es5 exports
与 es6 imports
混合使用,这有时会变得很奇怪!
export const example1 = async () => {
return 'example 1'
}
export const example2 = async () => {
return 'example 2'
}
// other file
import { example1, example2 } from '../../example'
return example1()
不过,如果您必须混合使用它们,请告诉我!我们也可以找到解决方案!
更多关于导出模块和可能出错的地方!
关于javascript - Nodejs - 如何在单独的文件中分组和导出多个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45781063/