Cannot use import statement outside a module

root@080eed5f661048:/myapp# cd prisma
root@080eed5f661048:/myapp/prisma# node seed.ts
/myapp/prisma/seed.ts:2
import {PrismaClient} from '@prisma/client'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1124:15)
    at Module._compile (node:internal/modules/cjs/loader:1160:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10)
    at Module.load (node:internal/modules/cjs/loader:1074:32)
    at Function.Module._load (node:internal/modules/cjs/loader:909:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:22:47

I am using bluestacks from remix, and seeing the files, has the same tsconfig, same package json, and for some reason it manages to compile it correctly.

@joseamica

The error you’re encountering occurs when you’re trying to use ES modules syntax (import/export) in a CommonJS environment.

To fix this issue, you can try the following steps:

  1. Make sure your tsconfig.json file has the following settings:
{
  "compilerOptions": {
    "module": "CommonJS",
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    ...
  },
  ...
}
  1. If you’re using Node.js 12 or later, you can add the following line to your package.json file:
{
  "type": "module",
  ...
}

This will enable ES modules syntax in your project.

  1. If you still encounter the issue, you can try changing your import statement to use the require syntax instead:
const { PrismaClient } = require('@prisma/client');

Make sure to apply these changes and re-run your script

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.