open-next icon indicating copy to clipboard operation
open-next copied to clipboard

feat: remove dev node_modules deps

Open khuezy opened this issue 2 years ago • 15 comments

fixes: #166

This reduces my zipped lambda from 31MB => 10MB

I investigated the use of next.config's outputFileTracingExcludes but that is hard to merge w/ the user's config file since the config is js and may contain functions - which we can't easily stringify. Doing a complex regex to replace is very tricky and brittle if the user's config file is non standard too...

So the solution is to do the node_modules cleanup AFTER next has built.

khuezy avatar Sep 20 '23 21:09 khuezy

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
open-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 20, 2023 9:50pm

vercel[bot] avatar Sep 20 '23 21:09 vercel[bot]

⚠️ No Changeset found

Latest commit: e8e8ea739a00bcca216cc66fc3ac611eecba7312

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

changeset-bot[bot] avatar Sep 20 '23 21:09 changeset-bot[bot]

@khuezy The docs mention that next 14+ doesn't have the issue anymore but I that doesn't seem to be the case for me. So this PR is still valuable

mathisobadia avatar Nov 06 '23 14:11 mathisobadia

Any progress on this?

seawatts avatar Dec 11 '23 03:12 seawatts

Any progress on this?

Not much, for now, you can try using outputFileTracingExcludes:

 outputFileTracingExcludes: {
   '*': [
     '**@swc/core**',
     '**esbuild**',
    '**uglify**',
      'webassemblyjs',
     '**sass**',
     '**caniuse-lite**',
     "@aws-sdk"
   ],
},

It's hard to know which to remove so it might be better for the user to manage that. For example, if you add "watchpack", it'll break server actions even though it looks like a dev dep.

khuezy avatar Dec 11 '23 20:12 khuezy

Any progress on this?

Not much, for now, you can try using outputFileTracingExcludes:

 outputFileTracingExcludes: {
   '*': [
     '**@swc/core**',
     '**esbuild**',
    '**uglify**',
      'webassemblyjs',
     '**sass**',
     '**caniuse-lite**',
     "@aws-sdk"
   ],
},

I'm trying to use this but I get a "RangeError: Maximum call stack size exceeded" error: https://github.com/vercel/next.js/issues/42641#issuecomment-1872592551

revmischa avatar Dec 30 '23 22:12 revmischa

Something change in the later version of next (not sure if bug or by design.) Using the "**" causes an infinite loop. You have remove the wildcard.

khuezy avatar Dec 30 '23 23:12 khuezy

Something change in the later version of next (not sure if bug or by design.) Using the "**" causes an infinite loop. You have remove the wildcard.

Hm, I tried

    outputFileTracingExcludes: {
      "*": [
        "@swc/core",
        "webpack",
        "docx",
       ]

And get the same error

revmischa avatar Dec 31 '23 01:12 revmischa

Maybe im doing something weird here, but on nextjs 14.1 and pnpm ... I've tried to manually remove the node-modules from the server function you have listed above and my lambda fails pretty hard.

{ "errorType": "Error", "errorMessage": "Cannot find module 'styled-jsx/package.json'\nRequire stack:\n- /var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js\n- /var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js", "code": "MODULE_NOT_FOUND", "requireStack": [ "/var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js", "/var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js" ], "stack": [ "Error: Cannot find module 'styled-jsx/package.json'", "Require stack:", "- /var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js", "- /var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js", " at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15)", " at resolve (node:internal/modules/helpers:188:19)", " at Object.<anonymous> (/var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js:38:32)", " at Module._compile (node:internal/modules/cjs/loader:1356:14)", " at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)", " at Module.load (node:internal/modules/cjs/loader:1197:32)", " at Module._load (node:internal/modules/cjs/loader:1013:12)", " at Module.require (node:internal/modules/cjs/loader:1225:19)", " at require (node:internal/modules/helpers:177:18)", " at Object.<anonymous> (/var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js:13:1)" ] }

enroly-mike avatar Jan 31 '24 16:01 enroly-mike

I've tried to manually remove the node-modules

Did you remove the entire folder? You should only remove the dev subfolders, not the entire node_modules

khuezy avatar Feb 04 '24 01:02 khuezy

nextjs v14.2.4 / open-next 3.0.2 -- still includes dev dependencies in standalone build.

This next config works and excludes unwanted dependencies (note: the setup for monorepo):

    outputFileTracingRoot: path.join(__dirname, '../../'),
    outputFileTracingExcludes: {
      '*': [
        './**/.prisma/client/libquery_engine-darwin*', // prisma mac binary
        './**/@swc/core-linux-x64-gnu*',
        './**/@swc/core-linux-x64-musl*',
        './**/@esbuild*',
        './**/webpack/',
        './**/rollup*',
        './**/terser*',
        './**/sharp*',
      ],
    },

AndreyMay avatar Jul 02 '24 04:07 AndreyMay