Next.js: Code elimination broken for module that exports frontend and Node.js

Created on 13 Aug 2020  路  5Comments  路  Source: vercel/next.js

Bug report

Describe the bug

The code elimination is broken, if you import from a file, which exports both frontend and Node.js code.

To Reproduce

Clone and run:
https://github.com/timsuchanek/build-cv-messing-around

Expected behavior

The code elimination should sort out the frontend and Node.js code correctly.

Screenshots

image

System information

  • OS: Ubuntu 20
  • Version of Next.js: 9.5.2
  • Version of Node.js: 14.8.0

Additional context

This is an isolated reproduction of a problem a Prisma user hit with running Prisma Client in getStaticProps https://github.com/prisma/prisma/issues/3252

Most helpful comment

I've tested that under a slightly different setup, using SQLite as a sort of extreme case (to avoid backend calls for data that is potentially configurable, like in a CMS - updates are done through git push -, but runtime access in readonly mode and without having to maintain it on a central server). In some settings it works, in others it doesn't.

Here's an example of stack trace when it doesn't work:

error - ./node_modules/@prisma/client/runtime/index.js:87:2855
Module not found: Can't resolve 'async_hooks'
null
Could not find files for / in .next/build-manifest.json

so it fails on the instruction {AsyncResource:Iy}=required("async_hook")

Note : I retried this morning, and the example works. So I'm not sure what happened. If I can reproduce, I'll publish a code example.

All 5 comments

As of right now it's not possible to have both client-side JS and Node.js-specific code as the tree shaking of getStaticProps / getStaticPaths works on the module level, it's not a whole program optimization. You can see what is removed and what is not here: https://next-code-elimination.now.sh/

The elimination is only applied to page files in the pages directory.

Btw in this case, we're not even having frontend code, but just an import of a json object, that is unused. Maybe fixing this case could already be a good first step.

Is it possible to test the scenario I described here in https://next-code-elimination.now.sh/ ?
From what I see, it would need a separate pane in there to be able to build a separate module.

I've also been experiencing that using Prisma. I'm exposing a basic REST API from my Next.js backend, and I want to just use my Prisma model types for static type checking.
My workaround now is to redefine my Prisma models as TypeScript types in another file. Would be nice to not have to maintain this code duplication.

I've tested that under a slightly different setup, using SQLite as a sort of extreme case (to avoid backend calls for data that is potentially configurable, like in a CMS - updates are done through git push -, but runtime access in readonly mode and without having to maintain it on a central server). In some settings it works, in others it doesn't.

Here's an example of stack trace when it doesn't work:

error - ./node_modules/@prisma/client/runtime/index.js:87:2855
Module not found: Can't resolve 'async_hooks'
null
Could not find files for / in .next/build-manifest.json

so it fails on the instruction {AsyncResource:Iy}=required("async_hook")

Note : I retried this morning, and the example works. So I'm not sure what happened. If I can reproduce, I'll publish a code example.

Was this page helpful?
0 / 5 - 0 ratings