Back

SyntaxError: Cannot use import statement outside a module <Cloud Functions>

  • 0
  • Functions
  • Web
  • Cloud
Bilo
27 Mar, 2024, 17:01

/usr/local/server/src/function/src/main.ts:1 import { Client, Users, Databases } from 'node-appwrite

i tried to switch from js to ts in cloud functions, i've changed the main.js to main.ts and also changed the package.json { "name": "starter-template", "version": "1.0.0", "description": "", "main": "src/main.ts", "type": "module", "scripts": { "format": "prettier --write ." }, "dependencies": { "node-appwrite": "^9.0.0" }, "devDependencies": { "prettier": "^3.0.0" } }

and also updated the Entrypoint in the function Configuration section src/main.ts but still didn't want to wrok

TL;DR
Developers encountering 'SyntaxError: Cannot use import statement outside a module' in Cloud Functions can't directly use TypeScript. It's suggested to consider using the 'bun' starter code, which is similar to TypeScript. To resolve, switch from .js to .ts in the project's package.json and change the 'main' entry accordingly. Additionally, ensure the 'type' is set to 'module'.
Binyamin
27 Mar, 2024, 17:08

You won't be able to use typescript like so out of the box in Node fucntion For this to work change index to js then copy this to your json file

TypeScript
{
  "name": "starter-template",
  "version": "1.0.0",
  "description": "",
  "main": "src/main.js",
  "type": "module",
  "scripts": {
    "format": "prettier --write ."
  },
  "dependencies": {
    "node-appwrite": "^11.1.1"
  },
  "devDependencies": {
    "prettier": "^3.0.0"
  }
}
Binyamin
27 Mar, 2024, 17:08

If you do want to use typescript I'll suggest creating bun function

Binyamin
27 Mar, 2024, 17:09

Check the bun starter code

Bilo
27 Mar, 2024, 17:11

thank you, will check that! so there is no wy to use typescript in nodejs ?

Binyamin
27 Mar, 2024, 17:12

Not right now, no

Binyamin
27 Mar, 2024, 17:12

But bun offers almost the same features

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