node.jsangular-climonoreponrwl-nxcode-sharing

How do I go about sharing a class instance in a monorepo?


I have a JavaScript / TypeScript monorepository generated using something similair to Angular CLI, I have 3 apps: frontend, server and a Discord bot.

The Discord bot is the core of this project, the bot is basically a discord.js class instance (Client), I want both my server and bot project to access the client's data.

I have created a library which exposes a database connection for my server and bot app, my question is what should I do with the class instance? Should I export the class instance in a library and write the logic for it in the bot app and be able to access it in the server? Since the library would export nothing more than

import { Client } from "discord.js"

export const client = new Client()

client.login(process.env.TOKEN)

I was wondering because I could create the logic for the bot inside the library as well but I feel like that is against going against the point of the structure, I would appreciate any feedback


Solution

  • Don't share the instance of the client, have the frontend and server packages import your class and create the instance themselves.