8000 🐛 Bug Report: Cloud Functions: cannot use import statement outside a module · Issue #6104 · appwrite/appwrite · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🐛 Bug Report: Cloud Functions: cannot use import statement outside a module #6104

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

Closed
2 tasks done
JorensM opened this issue Sep 1, 2023 · 14 comments
Closed
2 tasks done
Assignees
Labels
bug Something isn't working product / functions Fixes and upgrades for the Appwrite Functions.

Comments

@JorensM
Copy link
JorensM commented Sep 1, 2023

👟 Reproduction steps

  1. Create and deploy a basic Function with the Appwrite CLI
  2. Execute the function from the console
  3. See error in logs

👍 Expected behavior

It should execute the function without errors

👎 Actual Behavior

The logs of the function execution in the console show this:

/usr/code-start/stripePaymentIntent.js:1
import { Client } from 'node-appwrite';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:355:18)
    at wrapSafe (node:internal/modules/cjs/loader:1038:15)
    at Module._compile (node:internal/modules/cjs/loader:1072:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Function.Module._load (node:internal/modules/cjs/loader:828:14)
    at Module.require (node:internal/modules/cjs/loader:1012:19)
    at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
    at require (node:internal/modules/cjs/helpers:93:18)
    at /usr/local/src/server.js:55:28

Also if I remove the import statement, it gives me "unexpected token 'export' "

This is the code of my function:

import { Client } from 'node-appwrite';

// This is your Appwrite function
// It's executed each time we get a request
export default async ({ req, res, log, error }) => {
    
    log('test')

    return res.json('Success')


};

🎲 Appwrite version

Appwrite Cloud

💻 Operating system

Windows

🧱 Your Environment

I'm using Appwrite Cloud.

👀 Have you spent some time to check if this issue has been raised before?

  • I checked and didn't find similar issue

🏢 Have you read the Code of Conduct?

@JorensM JorensM added the bug Something isn't working label Sep 1, 2023
@ItzNotABug
Copy link
Member

Does it work if you use below format?

const {Client} = require('node-appwrite');

@JorensM
Copy link
Author
JorensM commented Sep 1, 2023

@ItzNotABug Thanks, this solves the error. I'm guessing though that it should work with ESM syntax as well, because the docs use ESM syntax.

But now I have another error: log is not a function I tried investigating and found that what gets passed to the cloud function is the following:

{
   "variables":{
      "publishable_key_test":"..", //My own env var
      "APPWRITE_FUNCTION_ID":"stripe-payment-intent",
      "APPWRITE_FUNCTION_NAME":"Stripe payment intent",
      "APPWRITE_FUNCTION_DEPLOYMENT":"...",
      "APPWRITE_FUNCTION_TRIGGER":"http",
      "APPWRITE_FUNCTION_PROJECT_ID":"...",
      "APPWRITE_FUNCTION_RUNTIME_NAME":"Node.js",
      "APPWRITE_FUNCTION_RUNTIME_VERSION":"16.0",
      "APPWRITE_FUNCTION_EVENT":"",
      "APPWRITE_FUNCTION_EVENT_DATA":"",
      "APPWRITE_FUNCTION_DATA":"",
      "APPWRITE_FUNCTION_USER_ID":"...",
      "APPWRITE_FUNCTION_JWT":"...",
      "INERNAL_EXECUTOR_HOSTNAME":"exc1"
   },
   "headers":{
      
   },
   "payload":""
}

It doesn't pass req, res, log, error as the docs show.

@tripathi-g
Copy link

that is because doc refers latest appwrite 1.4 but appwrite cloud is running on 1.1.2. That me be the issue.

@ItzNotABug
Copy link
Member

I believe your project must be a module in package.json.
That would support the syntax mentioned in the docs.

See: Check this: https://github.com/appwrite/templates/tree/main/node/starter
Also make sure to use the latest available Runtime version, see: https://appwrite.io/docs/functions-runtimes.

@JorensM
Copy link
Author
JorensM commented Sep 1, 2023

@ItzNotABug I tried setting type to module in package.json, and now I'm getting this error:

Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/code-start/createUser.js from /usr/local/src/server.js not supported.
Instead change the require of createUser.js in /usr/local/src/server.js to a dynamic import() which is available in all CommonJS modules.
    at Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
    at /usr/local/src/server.js:55:28

While running this code:

export default async (fns) => {
    console.log('aaa')
    console.log(fns)
}

@tripathi-g Thanks, Is there any place where I can see the docs for 1.1.2 ?

@stnguyen90
Copy link
Contributor

@JorensM, not anymore. Cloud will be updated to 1.4 soon. For now, you can refer to this for reference.

@stnguyen90 stnguyen90 added the product / functions Fixes and upgrades for the Appwrite Functions. label Sep 1, 2023
@stnguyen90 stnguyen90 self-assigned this Sep 1, 2023
@JorensM
Copy link
Author
JorensM commented Sep 1, 2023

@stnguyen90 Thanks, that's helpful! How soon is Cloud going to be updated?

@gewenyu99
Copy link
Contributor

@JorensM This isn't an Appwrite bug. Please update your package.json to be a module:

{
  // ...
  "type": "module",
  // ...
}

https://stackoverflow.com/questions/58211880/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module-when-import

@JorensM
Copy link
Author
JorensM commented Sep 2, 2023

@gewenyu99 thanks, I figured that out. But see #6104 (comment)

@gewenyu99
Copy link
Contributor

@gewenyu99 thanks, I figured that out. But see #6104 (comment)

Yes the documentation you're reading is for 1.4, not < 1.3 😅 The guide docs are always latest version.

Legacy docs, refer to references and select 1.3 or your version in the top right.

@JorensM
Copy link
Author
JorensM commented Sep 5, 2023

@gewenyu99 I meant the first part of this comment: #6104 (comment)

I changed type to module but am now getting a different error.

@stnguyen90
Copy link
Contributor

@comuneoOrg
Copy link

For me, I'm using Remix.run, and I had this error. On Remix, by their documentation:

Server-only code - **Remix will remove server-only code, but it can't if you have module side effects that use server-only code.**
Browser-only code - Remix renders on the server so your modules can't have module side effects or first-rendering logic that call browser-only APIs

So in other words, to use it properly, I needed to create a new file something.server.ts, this way it's a server-side file, that I can use without any problem.
I hope it solves your problem as well.

@gewenyu99
Copy link
Contributor

Closing this since there's no further activity and this is a caveat with Node.js imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working product / functions Fixes and upgrades for the Appwrite Functions.
Projects
None yet
Development

No branches or pull requests

6 participants
0