I am trying to learn how to use monorepos and using a turborepo in my project
all I simply want to do is create a package called db which exports a primsaClient which i can use in my other apps wherever required
here is how the structure looks like
main/
package.json
turbo.json
apps/
modelapp/
index.ts //prismaClient is imported in this file
package.json
packages/
db/
prisma/
index.ts //prismaClient is exported in this file
package.json
what I tried
exported primsa like this
import {PrismaClient} from '@prisma/client';
const prismaClient = new PrismaClient();
export default prismaClient
package.json for db looks like this
{
"name": "@copetech/db",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"exports": {
".": {
"require": "./index.ts",
"import": "./index.ts"
}
},
"devDependencies": {
"prisma": "5.18.0"
},
"dependencies": {
"@prisma/client": "5.18.0"
}
}
package.json for modelapp
{
"name": "model",
"version": "1.0.0",
"main": "src/index.ts",
"scripts": {
"build": "tsc",
"start": "node dist/index.js",
"dev": "tsc -b && node dist/index.js"
},
"dependencies": {
"@copetech/db": "*",
"@types/express": "^4.17.21",
"express": "^4.19.2",
"mongoose": "^6.0.12"
}
}
index.ts for model app
import express from "express"
const app = express();
import prismaClient from "@copetech/db";
app.get("/", (req, res) => {
prismaClient.$connect();
res.send("Hello World");
});
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
problems I am facing
- I want to make sure that packages in db is not built , It should just be there to be imported by any of my apps
- I am not getting any error while importing primsaClient from “@copetech/db” but I am running into problems while running npm run dev on the root folder
something like this
(node:27201) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/Users/nikhil/Desktop/monorepo/packages/db/index.ts:1
import {PrismaClient} from '@prisma/client';
^^^^^^
SyntaxError: Cannot use import statement outside a module
i tried using npm and yarn both and could not overcome this problem, I am also a beginner in typescript so not sure if im missing something important here