Back

[SOLVED] `Unexpected token 'export'` error in function

  • 0
  • Functions
  • Web
manazo
21 Sep, 2023, 13:27

I'm not sure if I'm doing anything wrong with the syntax. I've copied one of the example codes and removed the logic and left only 2 lines of log to test it. Below is the code

TypeScript
export default async function ({ req, res, log, error }) {
  log("Request received");

  log("--------");

  log("Request Object: ==>>", req);

  return res.send("Hello World");
}

any idea why I'm getting this error? any help will be appreciated

TL;DR
The user was getting an "Unexpected token 'export'" error in their function. Following a suggestion, they converted it to a module by changing the entry point filename from "index.js" to "index.mjs", which solved the issue. It was mentioned that the problem may have been caused by using a different syntax, as the function defaults to common js. The user also shared their package.json and inquired about the difference between manual deployment and deploying with the Appwrite cli. It was mentioned that the issue could be related to the Node version, as Appwrite currently uses Node v16 which is now end of life. The recommended solution
Binyamin
21 Sep, 2023, 13:44

To what version of Appwrite you're deploying this function?

manazo
21 Sep, 2023, 16:02

the latest version. 1.4.2

Binyamin
21 Sep, 2023, 16:46
  • How have you created the function?
  • What version is your Appwrite cli?
manazo
21 Sep, 2023, 20:15
  1. Functions tab -> create function -> manually -> uploaded the tar file -> specified entry point (index.js) -> create -> activate
  2. i did it manually from the console cuz it was only for testing to check what info i get in the context object
Guille
21 Sep, 2023, 20:38

It think it may be a node version problem

Drake
21 Sep, 2023, 20:39

Is this function from before 1.4?

Binyamin
21 Sep, 2023, 20:52

What is that tar file includes? It's recommend using the latest version of Appwrite cli > 4.0.0 and deploy a function using this method.

RAKU
21 Sep, 2023, 21:59

I am also facing the same issue 😦

Update: Changing the entry point filename from "index.js" to "index.mjs" solved the issue πŸ™‚

manazo
22 Sep, 2023, 02:58

That might be the case. appwrite currently uses Node v16 which is now end of life, according to this post

manazo
22 Sep, 2023, 02:59

nope. i copied this code from the official doc and removed all the logic and kept only 3 logs to test it out

Drake
22 Sep, 2023, 03:00

This is the function for the deployment. I meant did you create this function before or after 1.4?

manazo
22 Sep, 2023, 03:01

shown in the ss. node_modules doesn't have any modules actually and about deploying with Appwrite cli, I wonder what difference it makes when we do manual deployment. I've used the manual deployment before (before 1.4) and it worked well

manazo
22 Sep, 2023, 03:02

after 1.4

manazo
22 Sep, 2023, 03:08

Update: Changing the entry point filename from "index.js" to "index.mjs" solved the issue πŸ™‚ I just tried and it worked for me as well. but i wonder what the issue was in the first place

Drake
22 Sep, 2023, 03:10

What's in your package.json ?

manazo
22 Sep, 2023, 03:10
TypeScript
{
  "name": "trabo-server-functions",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
manazo
22 Sep, 2023, 03:26

yeah it works now. just like @RAKU suggestion, we needed to convert it to a module. but i wonder what's the problem when we don't keep it as a module?

Drake
22 Sep, 2023, 03:27

Maybe it defaults to common js which has a different syntax

manazo
22 Sep, 2023, 03:29

[SOLVED] Unexpected token 'export' error in function

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more