I have a project that is using TypeScript, Prisma & NextJS.
I want to see my database with prisma using the yarn prisma db seed
command.
However, I am facing issues.
The problem is the file that I am using to seed my database prisma/seed.ts
is using an import
statement and that file is subsequently importing a .mjs
file.
So I get the following error in the console when I run yarn prisma db seed
import { env } from '../../env/server.mjs';
^
Error [ERR_REQUIRE_ESM]: require() of ES Module
Here is my package.json. In my package.json I set up a script to use different compiler options for commonjs modules. I also needed to set the prisma.seed
property to define which file is the seed file.
{
"name": "...",
"version": "0.1.0",
"private": true,
"scripts": {
"ts-node": "ts-node --compiler-options '{\"module\": \"CommonJS\"}'",
},
"dependencies": {
"@prisma/client": "^4.5.0",
"next": "13.0.2",
"react": "18.2.0",
"react-dom": "18.2.0",
},
"devDependencies": {
"@babel/preset-env": "7.12.1",
"@types/node": "^18.0.0",
"@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5",
"prisma": "^4.5.0",
"ts-node": "^10.9.1",
"typescript": "^4.8.4",
},
"prisma": {
"seed": "yarn ts-node prisma/seed.ts"
}
}
Here is my tsconfig.json
file to configure TypeScript settings. I tried adding ts-node.esm
set to true
here but doesn't seem to work, so maybe some other config property needs amended/added.
{
"compilerOptions": {
"target": "es2017",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"noUncheckedIndexedAccess": true,
},
"ts-node": {
"esm": true
},
"include": ["next-env.d.ts", "types/**/*.ts", "next-auth.d.ts", "**/*.ts", "**/*.tsx", "**/*.js", "**/*.cjs", "**/*.mjs"],
"exclude": ["node_modules"]
}
And here is my prisma/seed.ts
file.
import { prisma } from '../src/server/db/client';
const main = async () => {
console.log('seeding');
};
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
The import for ../src/server/db/client
is the file that includes the .mjs
import. That is causing the error.
I need the script to handle both allowing import statements of TypeScript files but ALSO .mjs files. Not one or the other, I need both for the script to run successfully, I've found ways to remove the Error [ERR_REQUIRE_ESM]: require() of ES Module
error but then get the following error instead
import { prisma } from '../src/server/db/client';
^^^^^^
SyntaxError: Cannot use import statement outside a module
For example;
Updating the ts-node
script to be
"ts-node": "ts-node --compiler-options '{\"module\": \"ESNext\"}'",
and updating the prisma.seed
to be
"prisma": {
"seed": "ts-node prisma/seed.ts"
}
Any help is greatly appreciated
To run ES Modules, you can add the following property in the top-level of your
package.json
file: