The Issue I need to solve
Simply put, I need to check if a node package is installed at runtime. The reason is that I’m building a new toolkit ZenStack on top of Prisma with a powerful access control layer and API generation. Since it is built above Prisma, when running the CLI provided by ZenStack, it must check whether Prisma has already been installed. Otherwise, it would prompt the user to install Prisma first.
Get the answer
Let’s try to ask ChatGPT directly:
Cool! The answer looks compelling. Problem solved!
The side effect
It does work, but it brings one side effect. In my CLI project, I use async-exit-hook to handle uncaught errors like the below:
// You can hook uncaught errors with uncaughtExceptionHandler(), consequently adding
// async support to uncaught errors (normally, uncaught errors result in a synchronous exit).
exitHook.uncaughtExceptionHandler(err => {
console.error(err);
});
After the require(’prisma’) is executed successfully, which means Prisma is there, if later in other code you throw any uncaught exception, the uncaughtExceptionHandler would never be triggered. But if it requires any other existing module like require(’uuid’), then the uncaughtExceptionHandler would still be triggered as before.
Although I haven’t found the real cause, from the above, it looks like some code is running when Prisma is imported. I’m not sure whether Prisma provides any flag to control it; even if it does, it still doesn’t sound like a clean solution to me because it is like Pandora's box. You won’t know what gets executed.
Clean solution
The most direct way to avoid a side effect is to use a separate process. So, how would you do that if the job is assigned to you? I will use the npm command. So let’s ask the omniscient again:
This looks like a clean solution to me.
Push harder
As good developers, we should always try to think further. What if the npm command is not installed or is broken? If we know how npm finds the module, we could do that ourselves.
Let’s ask again:
As the npm command obviously won’t execute any code for the package, it does find the name and version as the answer:
It will display the names and versions of all the installed packages
So, where does it get the version information? Of course, from package.json file. Actually, you can easily verify it by creating dummy-package folder under node_modules, and then creating the package.json file with the below content:
{
"name": "dummy-package",
"version": "9.9.9"
}
Then, after running npm list --depth=0 dummy-package, you could see the package info:
helloworld@1.0.0 /Users/jiasheng/branch/helloworld
└── dummy-package@9.9.9 extraneous
Therefore, instead of requiring the module like the original solution, we could change it to require the package.json file like:
const prisma:any = require('prisma/package.json')
Not only does it eliminate the side effects, but you could also get more information about that package, like the version, etc.
Final Words
Anyway, you can see ChatGPT really could help us a lot, even write the code for us, but it is we who actually think about and resolve the issue thoroughly. Coming to the thought that AI would replace developers, I think Google should worry about that rather than us. 😉
Follow up
There is one comment left by Anthony that brings up one “side effect” of using ChatGPT “while ChatGPT arguably helped you solve your problem, you still failed to actually learn what was going on”, which I can’t agree more. Therefore, I investigated the source code of Prisma a little bit. It turns out that the side effect of the first solution ChatGPT gives is that when Prisma is required, it registers unhandledRejection event handler in the below line:
https://github.com/prisma/prisma/blob/main/packages/cli/src/bin.ts#L57
process.on('unhandledRejection', (e) => {
debug(e)
})
The exception thrown later is in the Promise handler, so it got intercepted by this event handler. Therefore, another way of fixing it is to also register unhandledRejection event handler using exitHook like below:
// You can hook unhandled rejections with unhandledRejectionHandler()
exitHook.unhandledRejectionHandler(err => {
console.error(err);
});
But I would still stick to the required package.json solution as it has no side effects at all. 😁
ZenStack is our open-source TypeScript toolkit for building high-quality, scalable apps faster, smarter, and happier. It centralizes the data model, access policies, and validation rules in a single declarative schema on top of Prisma, well-suited for AI-enhanced development. Start integrating ZenStack with your existing stack now!



Latest comments (35)
Doesnt help at all instruction not clear....
Yes, it's a tool, not a replacement.
You order from menu, but have no idea how it was cooked.
You didn't go all the way to solve the problem.
ChatGPT doesn't help you, it harms you. He does your work for you. So maybe hire him instead of a lazy developer?
Chatgpt + Unknown breakthrough = everyone is doomed. The unknown breakthrough could be anything.. something that makes us think about everything differently.. some new approach... Could happen any day...could take decades..
What I find interesting is that while ChatGPT arguably helped you solve your problem, you still failed to actually learn what was going on. The reason your unhandledError hook didn't fire has nothing to do with prisma potentially having a flag or doing something odd. The solution ChatGPT provided involves handling the module not found exception... therefore by definition your unhandled hook is never going to fire. The whole subprocess hack that you're calling a _Clean Solution _ is completely unnecessary and misleading to others who may unfortunately stumble upon this article.
A better approach to become an adequate developer... always aim to understand a situation, then solve it. Searching for the solution seems like the fast path but in the end you're left with buggy, ineffective code that kinda/sorta sometimes works... all because you don't truly understand what's happening.
It's like going to the doctor saying your leg hurts. The doctor gives you morphine and all the sudden, you feel better! Problem solved!!! Except your leg was broken and you needed a cast. You can't just treat the symptom as a doctor, or a developer.
That said, maybe ask ChatGPT about unhabdled vs handled exceptions and understand how, why, and when the unhandled exception filter would get executed. Best of luck
Hi Anthony, it seems that l didn’t specify the problem clearly, thanks for pointing it out. I have updated the corresponding content to make it clear.
As you can see I still believe it’s caused by some code of Prisma running when importing the Prisma module because using any other existing module would be fine.
Anyway, I think you are right that I still haven’t truly understand what’s happening under the hood 😂. I think it requires some investigation of the source code of Prisma. I will take a look later and update the result here.
Updated in the end of the post.
It took me 5 days to steer it to making the blender script in needed and during that time it needed a lot of corrections from me, it didn’t know what was correct and modern and wasn’t aware of API changes until I told it. It also looped around a solution quite a few times or flat out misunderstood this visual task… it’s still incredible
I like your attitude, It’s all about how we can utilize it. 😄
i asked same question and answear was correct
The answer is probabilistic. I actually have no doubt that AI could answer this kind of definite question correctly as evolving.
I tried te debug a lua app concerning merging two lists, all the code the chat gave seemed logical, but variable names, logic, etc.. was all wrong in a way that led to weird bugs.
It can give hints, but often it will reference constants from an entire different language or app
Use it as a rubber duck, not to generate your code.
exacly!
There is an extension that can assist you in displaying ChatGPT responses alongside the search results from Google, Bing, and DuckDuckGo. Maybe you should try it: https://chrome.google.com/webstore/detail/chatgpt-for-search-engine/feeonheemodpkdckaljcjogdncpiiban/related?hl=en-GB&authuser=0
In other words, careful intelligent use turns it into another developer amplifier rather than a developer replacer.
I like it.
Nice one, it’s concise and comprehensive!👍