drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: `SyntaxError: Cannot use import statement outside a module` with drizzle-kit

Open binajmen opened this issue 1 year ago • 3 comments

What version of drizzle-orm are you using?

^0.27.0

What version of drizzle-kit are you using?

^0.19.2

Describe the Bug

I have the following syntax error when trying to use drizzle-kit:

➜  api git:(main) ✗ pnpm run db:generate

> [email protected] db:generate /apps/api
> drizzle-kit generate:pg

drizzle-kit: v0.19.2
drizzle-orm: v0.27.0

No config path provided, using default 'drizzle.config.ts'
Reading config file '/apps/api/drizzle.config.ts'
/node_modules/.pnpm/[email protected][email protected]/node_modules/drizzle-orm/pg-core/index.mjs:1
import { M as TableAliasProxyHandler, N as PgColumnBuilder, e as entityKind, O as PgColumn, U as pgTableWithSchema, X as pgViewWithSchema, Y as pgMaterializedViewWithSchema, i as is } from '../alias-3edfcde9.mjs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1048:15)
    at Module._compile (node:internal/modules/cjs/loader:1083:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Object.newLoader [as .mjs] (/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:8506:13)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at Module.require (node:internal/modules/cjs/loader:1021:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/apps/api/src/db/schema/profiles.ts:2:31)

Expected behavior

No response

Environment & setup

  • pnpm
  • monorepo
  • Mac OS
// package.json
{
  "name": "api",
  "private": true,
  "version": "1.0.0",
  "description": "",
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc",
    "start": "tsc & node .",
    "dev": "tsc -w & nodemon .",
    "test": "echo \"Error: no test specified\" && exit 1",
    "db:generate": "drizzle-kit generate:pg",
    "db:migrate": "ts-node scripts/migrate"
  },
  "keywords": [],
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "drizzle-orm": "^0.27.0",
    "express": "^4.18.2",
    "helmet": "^7.0.0",
    "morgan": "^1.10.0",
    "postgres": "^3.3.5"
  },
  "devDependencies": {
    "@types/body-parser": "^1.19.2",
    "@types/cors": "^2.8.13",
    "@types/express": "^4.17.17",
    "@types/morgan": "^1.9.4",
    "@types/node": "^20.3.2",
    "drizzle-kit": "^0.19.2",
    "nodemon": "^2.0.22",
    "typescript": "^5.0.4"
  }
}
// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "esModuleInterop": true,
    "strict": true,
    "target": "es6",
    "noImplicitAny": true,
    // "skipLibCheck": true,
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "*": ["node_modules/*"]
    }
  },
  "exclude": ["node_modules", "dist", "drizzle"],
  "include": ["src/**/*", "drizzle.config.ts"]
}

binajmen avatar Jun 27 '23 05:06 binajmen

Any update or workaround for this? Running into the same issue.

etd-23 avatar Aug 14 '23 19:08 etd-23

Drizzle Kit can't be run in esm mode, but we are working on making it possible

AndriiSherman avatar Feb 05 '24 18:02 AndriiSherman

I found a dirty workaround for the meantime:

  • Install jiti
  • Rename node_modules/drizzle-kit/bin.cjs to bin.js
  • Run jiti ./node_modules/drizzle-kit/bin.js generate:sqlite

zsilbi avatar May 12 '24 15:05 zsilbi

Has there been any update on this because this breaks entire support for Expo because react native has ActionSheetIOS which uses imports with ESM syntax

SuzuyaBish avatar Oct 17 '24 07:10 SuzuyaBish