Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulator : SyntaxError: await is only valid in async functions and the top level bodies of modules #581

Open
merri-ment opened this issue Nov 12, 2023 · 1 comment
Labels

Comments

@merri-ment
Copy link

merri-ment commented Nov 12, 2023

Bug report

Describe the bug

In the Firebase Emulator locally, i encounter the following error when the function createCustomer is run;

functions: Failed to initialize and load triggers. This shouldn't happen: await is only valid in async functions and the top level bodies of modules
⚠  Your function was killed because it raised an unhandled error.
⚠  /Users/me/.cache/firebase/extensions/invertase/[email protected]/functions/lib/index.js:50
const subscription = await stripe.subscriptions.retrieve(subscriptionId, {
                     ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1126:15)
    at Module._compile (node:internal/modules/cjs/loader:1162:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)
    at Module.require (node:internal/modules/cjs/loader:1100:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at loadTriggers (/opt/homebrew/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:584:25)
    at main (/opt/homebrew/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:637:32)
⚠  We were unable to load your functions code. (see above)
   - It appears your code is written in Typescript, which must be compiled before emulation.
   - You may be able to run "npm run build" in your functions directory to resolve this.
⚠  functions: Failed to initialize and load triggers. This shouldn't happen: await is only valid in async functions and the top level bodies of modules
⚠  Your function was killed because it raised an unhandled error.
⬢  functions: Failed to handle request for function us-central1-ext-firestore-stripe-payments-createCustomer

I have tried running different node versions 16 and 18, that did not help.
I'm reluctant to edit the source file and wrap the await in a async func.

The only thing that resolved the issue was to downgrade to "stripe/[email protected]".

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Spin up Sprite CI - stripe listen --api-key rk_test_*********** --forward-to http://127.0.0.1:5001/{project-name}/{firebase-server}/ext-firestore-stripe-payments-handleWebhookEvents
  2. Install ext - firebase ext:install stripe/firestore-stripe-payments --project={project-name}
  3. Spin up Firebase Emulator CI - firebase emulators:start --project={project-name}
  4. Spin up my app - npm run dev
  5. Save a product in my Sprite developer admin Test-Mode environment

By saving a Product the 'product.updated' event will be trigger from Sprite and received by the invertase/firestore-stripe-payments Extension, where it fails.

@merri-ment
Copy link
Author

merri-ment commented Nov 12, 2023

One of the error suggestions is to npm run build in the firestore-stripe-payments functions directory.
I set node to 14 as per the package.json and ran build in firebase/extensions/invertase/[email protected]/functions to see what happens, but quite a few errors broke the build.


npm run build                     

> firestore-stripe-payments@ build /Users/me/.cache/firebase/extensions/invertase/[email protected]/functions
> npm run clean && tsc


> firestore-stripe-payments@ clean /Users/me/.cache/firebase/extensions/invertase/[email protected]/functions
> rimraf lib

src/index.ts:1:5 - error TS1128: Declaration or statement expected.

1     .collection(config.customersCollectionPath)
      ~

src/index.ts:111:1 - error TS1128: Declaration or statement expected.

111 };
    ~

src/index.ts:969:1 - error TS1005: '}' expected.

969 
    

  src/index.ts:965:21
    965 ): Promise<void> => {
                            ~
    The parser expected to find a '}' to match the '{' token here.


Found 3 errors in the same file, starting at: src/index.ts:1```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants