Code elimination broken for module that exports frontend and Node.js
See original GitHub issueBug 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
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:
- Created 3 years ago
- Reactions:17
- Comments:11 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.