首页 > 解决方案 > Nodejs 导出聚合引发语法错误

问题描述

我有许多较小的模块构成服务器的完整 API,每个模块都在它自己的文件中。但是,从单独的文件中导入每个模块相当烦人,因此,我想使用导出聚合来简单地从单个中央模块导入它们。

根据 MDN,该export语句应该能够处理以下聚合语法:

// Aggregating modules
export * from …; // does not set the default export
export * as name1 from …;
export { name1, name2, …, nameN } from …;
export { import1 as name1, import2 as name2, …, nameN } from …;
export { default } from …;

但是,我一直无法使用这种导出语法;特别是export * as name1 from ...语法,它会引发Unexpected export specifier type错误。

我能够使用的上述唯一形式是export * from ...,但这并不是特别有用......

这就是我在模块中导出 API 的方式:

// user.js - more modules export APIs the same way
export const loginWithEmail = ({ params }) => { /* ... */ }
export const loginWithPhoneNumber = ({ params }) => { /* ... */ }
// ... more exports

这就是我在中央模块中导出这些模块的方式

// api.js - resides in the same location as the separate modules
export * as User from "./user";
export * as Vehicle from "./vehicle";
// ... more exports

有人可以帮我弄清楚我做错了什么吗?

该环境由 VSCode 1.41、NodeJS 13、Expo SDK36、React 16.9、React-Native 0.61、JSX、ES8+ 和 eslint+babel-eslint 组成。

标签: javascriptnode.jsexportaggregate

解决方案


推荐阅读