Prisma-client-js: Prisma client doesn't work with SQLite and Next on Zeit Now

Created on 26 Jan 2020  路  15Comments  路  Source: prisma/prisma-client-js

Details

When I'm trying to make few API calls with Prisma client js within getServerSideProps on Next js, I get the following error.

Module not found: Can't resolve 'child_process' in '/Users/<username>/open-source/test-blog/node_modules/@prisma/photon/runtime'

Here's the full log

Module not found: Can't resolve 'child_process' in '/Users/<username>/open-source/test-blog/node_modules/@prisma/photon/runtime'
ModuleNotFoundError: Module not found: Error: Can't resolve 'child_process' in '/node_modules/@prisma/photon/runtime'
    at /node_modules/webpack/lib/Compilation.js:925:10
    at /node_modules/webpack/lib/NormalModuleFactory.js:401:22
    at /node_modules/webpack/lib/NormalModuleFactory.js:130:21
    at /node_modules/webpack/lib/NormalModuleFactory.js:224:22
    at /node_modules/neo-async/async.js:2830:7
    at /node_modules/neo-async/async.js:6877:13
    at /node_modules/webpack/lib/NormalModuleFactory.js:214:25
    at /node_modules/enhanced-resolve/lib/Resolver.js:213:14
    at /node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at /node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:44:7
    at /node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:15:1)
    at /node_modules/enhanced-resolve/lib/Resolver.js:285:5
    at eval (eval at create (/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
    at /node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:67:43

If you'd like to test this, you can replace getStaticProps by getServerSideProps here: https://github.com/smakosh/next-prisma2-now/blob/de749a6347de5f158e71189ad0cd2d89b7b83c01/pages/index.js#L6

kinquestion tectypescript

Most helpful comment

Ok to sum up the findings:

  1. Zeit, Next.js and Prisma work perfectly together. I just had a typo in my example, but if you use this, it works in Now:
    ```ts
    import React from 'react'
import { PrismaClient } from '@prisma/client'

export const getServerSideProps = async () => {
  try {
    const prisma = new PrismaClient()

    const users = await prisma.user.findMany()

    return {
      props: {
        users,
      },
    }
  } catch (err) {
    console.log(err)
    return {
      props: {
        error: err.message,
        users: [],
      },
    }
  }
}
```
The recommended way to use Prisma in Zeit is using MySQL or Postgres.
  1. A completely separate issue is getting SQLite running in Zeit Now in general.
    That's not an issue of Prisma, but of Zeit Now using Lambda under the hood, which doesn't allow writing to any directory besides /tmp.
    That means, if you just copy the SQLite file into your bundle, the database driver can't start, as it's read-only.

The solution for that obviously is, that you put the SQLite file into /tmp.
I did that here to demonstrate that it works:
https://github.com/timsuchanek/next-prisma-now-sqlite

All 15 comments

I can confirm this but getServerSideProps is a new and unstable feature of next. We would like to get it more stabilized and test Prisma with next internally.

Marking this as a bug, not sure it would counted as a feature.

@smakosh were you able to get it working?

Seems to fail when building on ZEIT Now, here are the logs

11:04:17.790  [4/4] Building fresh packages...
11:04:20.801  $ prisma2 generate
11:04:21.950  Error: Schema parsing
11:04:21.950  error: No such argument.
11:04:21.950    -->  schema.prisma:15
11:04:21.950     | 
11:04:21.950  14 |   published Boolean @default(false)
11:04:21.950  15 |   author    User?   @relation(fields: [authorId], references: [id])
11:04:21.950     | 
11:04:21.965  error Command failed with exit code 1.
11:04:21.965  info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
11:04:21.995  Error: Exited with 1
11:04:21.995      at ChildProcess.<anonymous> (/zeit/f8b3553cce6cf4b4/.build-utils/node_modules/@now/build-utils/dist/index.js:31349:24)
11:04:21.995      at ChildProcess.emit (events.js:223:5)
11:04:21.995      at ChildProcess.EventEmitter.emit (domain.js:475:20)
11:04:21.995      at maybeClose (internal/child_process.js:1021:16)
11:04:21.995      at Process.ChildProcess._handle.onexit (internal/child_p

Locally when running now dev it seems to work

Worked fine after upgrading prisma, not sure what's causing the 500 issue when navigating to /posts
https://prisma-test.now.sh/
Source: https://github.com/smakosh/next-prisma2-now

Got this from the real time logs

[GET] /api/posts
11:47:28:10
2020-04-09T10:47:28.115Z    2a8fea17-3fe3-45fd-b353-54261f724e52    ERROR   PrismaClientUnknownRequestError: 
Invalid `prisma.post.findMany()` invocation:
Error in connector: Error querying the database: unable to open database: /var/task/prisma/dev.db
    at PrismaClientFetcher.request (/var/task/node_modules/@prisma/client/runtime/index.js:1:52405)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

as @steebchen mentioned it seems using sqlite on ZEIT is an issue

I'm getting the same error when trying to run prisma client in the browser. I know this is not supposed to work like this. Just wondering, do you think it's a good idea to make prisma-client environment-agnostic so that it can be run both server- and client-side? May be it could use fetch to return data over http when it's run in the browser?

It's not run on the browser @yantakus, nothing is included on the browser js bundle, you can see that here: https://next-code-elimination.now.sh/

@smakosh I wasn't talking about your code. I get the same error in my own app. And then I realized that in my case prisma-client is used in such a place, which is run only in the browser and it's absolutely sane that it doesn't work. So I just wanted to share the idea of making prisma-client more flexible and able to execute in the browser. But now I see this is most probably a silly idea, because we need an http server for this and there's a lot of other problems that are out of scope of this project.

Getting a similar error. Using postgres but its inside a docker container

./node_modules/@prisma/client/runtime/index.js
Module not found: Can't resolve 'child_process' in 'node_modules/@prisma/client/runtime'

EDIT: FIgured it out must use import { PrismaClient } from '@prisma/client'
instead of const { PrismaClient } = require('@prisma/client') with next.js

Thanks for reporting! This is a bug in Next.js, I created an issue there: https://github.com/zeit/next.js/issues/11950

Ok to sum up the findings:

  1. Zeit, Next.js and Prisma work perfectly together. I just had a typo in my example, but if you use this, it works in Now:
    ```ts
    import React from 'react'
import { PrismaClient } from '@prisma/client'

export const getServerSideProps = async () => {
  try {
    const prisma = new PrismaClient()

    const users = await prisma.user.findMany()

    return {
      props: {
        users,
      },
    }
  } catch (err) {
    console.log(err)
    return {
      props: {
        error: err.message,
        users: [],
      },
    }
  }
}
```
The recommended way to use Prisma in Zeit is using MySQL or Postgres.
  1. A completely separate issue is getting SQLite running in Zeit Now in general.
    That's not an issue of Prisma, but of Zeit Now using Lambda under the hood, which doesn't allow writing to any directory besides /tmp.
    That means, if you just copy the SQLite file into your bundle, the database driver can't start, as it's read-only.

The solution for that obviously is, that you put the SQLite file into /tmp.
I did that here to demonstrate that it works:
https://github.com/timsuchanek/next-prisma-now-sqlite

Thanks @timsuchanek!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FluorescentHallucinogen picture FluorescentHallucinogen  路  3Comments

divyenduz picture divyenduz  路  3Comments

divyenduz picture divyenduz  路  3Comments

esistgut picture esistgut  路  4Comments

divyenduz picture divyenduz  路  3Comments