question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:17
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
fimbaultcommented, Oct 15, 2020

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.

7reactions
timneutkenscommented, Aug 13, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to detect dead code in a frontend project - LogRocket Blog
Explore different approaches to detect dead code in your frontend project ... modules to organize and reuse code via imports and exports.
Read more >
Node Module Exports Explained - freeCodeCamp
For Node.js, this process of code sharing – both within individual projects and in external npm dependencies – is facilitated using module.
Read more >
Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >
Module not found: Can't resolve 'fs' in Next.js application
Client + server code. import Link from 'next/link' export function ... I copied the "min.js" of the node module from the node_modules and ......
Read more >
Getting Started with (and Surviving) Node.js ESM
For browser and front-end application authors, the modern answer has been ECMAScript Modules ("ESM") features like import and export for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found