5

IN my React project I have need to share models(typescript interfaces in my case) between 3 typescript projects. So I opted for bit.env and imported all my models to https://bit.dev/model/index/~code and its all good.

Then I had a need to verify fields in my model so I added utility functions to https://bit.dev/model/index/~code#util.ts and deployed the same to bit.env

I started facing following error when I tried to use this helper function in my project #2 (place where I keep firebase cloud functions).

/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)

My code in project #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
            }

As pointed out in the code above I can use all interfaces from that model project but helper function throwing error message.

I referred to these articles and links

  1. Getting Unexpected Token Export
  2. https://medium.com/the-node-js-collection/an-update-on-es6-modules-in-node-js-42c958b890c

But none of them seems to work(also I was bit confused which project needs transpilation shared model or Project #2 in my case?). I can understand that this is some problem with node where it cant recognise ES6 module and requires some sort of intermediate transpilation. But I am wondering how all interfaces with similar export statement are working very fine. I would like to understand what am I missing here.

2
  • what version of typescript are you using? please give version local to the project if you have one, otherwise global version? Commented May 28, 2020 at 3:16
  • 1
    3.9.3 in both projects Commented May 28, 2020 at 6:27

2 Answers 2

2

NodeJS uses the CommonJS way of loading packages by default. With newer version of NodeJS you do get the Module functionality of JavaScript using either .mjs files or telling the engine the project is a module-based project.

I don't know if FireBase uses the latest Node versions, but this should at least explain why you're seeing that error. JS got support for Export long after NodeJS had already started wrapping things in CommonJS which is why it was such a problem to implement. Legacy support is almost always painful.

You can fix this by changing to CommonJS, or following the steps outlined in the NodeJS ESM page linked.

As for why this is needed for exported functions but not for exported Interfaces, it's because an Interface compiled to JS from TS is... nothing. Literally nothing. JS doesn't have a typing system, and so when TS gets compiled, anything that isn't needed is resolved to be excluded from compilation to the JS target.

Sign up to request clarification or add additional context in comments.

5 Comments

yes, that should be fine. That should tell it to compile to CommonJS and then it should work with NodeJS no problem.
For functions I use exact same format. I tried what has been mentioned above (adding tsconfig and set commonJS) but same outcome. What I am wondering is how come same 'export' style is working for interfaces but not for simple const/function? Thank you Robert
interfaces are TS, and when compiled often aren't included because JS has no need for them.
updated answer to include why interfaces aren't compiled to JS
2

It seems answer lies within bit.dev

"bit": {
    "env": {
      "compiler": {
        "bit.envs/compilers/[email protected]": {
          "rawConfig": {
            "tsconfig": {
              "compilerOptions": {
                "target": "ES5",
                "module": "CommonJS"
              }
            }
          }
        }
      }
    },
    "componentsDefaultDirectory": "src/{name}",
    "packageManager": "npm"
  },

Solved my issue by taking care of transpilation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.