Back

Cannot use import statement outside a module

  • 0
  • Functions
  • Cloud
louga31
26 Jan, 2024, 16:35

I'm migrating my typescript functions from 1.3 to 1.4 Before uploading with the cli I'm building them

I'm getting this error:

TypeScript
/usr/local/server/src/function/dist/main.js:1
import { Query } from "node-appwrite";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Module._load (node:internal/modules/cjs/loader:827:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at 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:102:18)
    at execute (/usr/local/server/src/server.js:123:28)

My package.json is configured like this:

TypeScript
{
  "name": "appwrite-function",
  "version": "1.0.0",
  "description": "",
  "main": "main.ts",
  "type": "module",
  "scripts": {
    "build": "tsc"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "node-appwrite": "^9.0.0",
    "utils": "file:../utils"
  },
  "devDependencies": {
    "@types/node": "^20.4.7",
    "prettier": "^3.0.0",
    "typescript": "^5.1.6"
  }
}

My tsconfig.json:

TypeScript
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "rootDir": "",
    "resolveJsonModule": true,
    "allowJs": true,
    "outDir": "dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "noImplicitAny": true,
    "skipLibCheck": true
  }
}
TL;DR
The developer is experiencing an error message stating "Cannot use import statement outside a module" when trying to build their TypeScript functions. They have migrated from TypeScript 1.3 to 1.4 and are building their functions before uploading them with the CLI. The error is occurring in the main.js file at line 1, specifically when using the import statement for "node-appwrite". The developer has confirmed that their function is defined as a module. To resolve this issue, the developer can try the following: 1. Check the package.json file: - Ensure the "type" field is set to "module" to enable ES module
louga31
26 Jan, 2024, 16:59

I can't manage to find why this is causing issues, as my function is actually defined as a module

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