打字稿:语法错误:意外的标记“导出”

vin*_*h10 5 javascript node.js typescript babeljs

在我的 React 项目中,我需要在 3 个打字稿项目之间共享模型(在我的例子中是打字稿接口)。所以我选择了 bit.env 并将所有模型导入到https://bit.dev/model/index/~code,一切都很好。

然后我需要验证模型中的字段,因此我将实用程序函数添加到https://bit.dev/model/index/~code#util.ts并将其部署到 bit.env

当我尝试在我的项目#2(我保存 firebase 云函数的地方)中使用此辅助函数时,我开始遇到以下错误。

/Users/vinoth.gopu/Documents/mine/oss/Cloud/functions/node_modules/@bit/model.index/dist/index.js:1
export * from './admin';
^^^^^^

SyntaxError: Unexpected token 'export'
    at wrapSafe (internal/modules/cjs/loader.js:1101:16)
    at Module._compile (internal/modules/cjs/loader.js:1149:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1205:10)

Run Code Online (Sandbox Code Playgroud)

我在项目 #2 中的代码

export const placeOrder = functions
    .runWith({
        memory:'256MB'
    })
    .region(cloudConfig.firebase.region)
    .firestore
    .document('Orders/{OrderId}')
    .onCreate((snap, context) => {
        const orderDetails = snap.data() as UserOrders;

        try {
            //check if all fields of interface implemented
            if(isOrder(orderDetails)) { // PROBLEM HERE
                 //do something
            }

Run Code Online (Sandbox Code Playgroud)

正如上面代码中所指出的,我可以使用该模型项目中的所有接口,但辅助函数会抛出错误消息。

我参考了这些文章和链接

  1. 获得意外的令牌导出
  2. https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c

但它们似乎都不起作用(我也有点困惑哪个项目需要转译共享模型或项目 #2 在我的例子中?)。我可以理解这是节点的一些问题,它无法识别 ES6 模块并且需要某种中间转译。但我想知道具有类似export声明的所有接口如何工作得很好。我想了解我在这里缺少什么。

vin*_*h10 2

看来答案就在 bit.dev 中

"bit": {
    "env": {
      "compiler": {
        "bit.envs/compilers/react-typescript@3.1.52": {
          "rawConfig": {
            "tsconfig": {
              "compilerOptions": {
                "target": "ES5",
                "module": "CommonJS"
              }
            }
          }
        }
      }
    },
    "componentsDefaultDirectory": "src/{name}",
    "packageManager": "npm"
  },
Run Code Online (Sandbox Code Playgroud)

通过处理转译解决了我的问题。