netlify / zip-it-and-ship-it

Intelligently prepare Node.js Lambda functions for deployment
https://www.npmjs.com/package/@netlify/zip-it-and-ship-it
MIT License
316 stars 35 forks source link

Breaks when using pnpm as it cannot find module 'follow-redirects' #1311

Open colinaut opened 1 year ago

colinaut commented 1 year ago

I am using pnpm for my 11ty site plus a few serverless functions. When Netlify is packaging my functions it errors out. It doesn't do this when I build locally only when it is built on Netlify. Note I don't have zip-it-and-ship-it in my package.json — not needed as this is a built in Netlify function. I switched to npm and it works fine so definitely a pnpm issue.

3:17:23 PM:   Dependencies installation error                               
3:17:23 PM: ────────────────────────────────────────────────────────────────
3:17:23 PM: ​
3:17:23 PM:   Error message
3:17:23 PM:   A Netlify Function failed to require one of its dependencies.
3:17:23 PM:   Please make sure it is present in the site's top-level "package.json".
​
3:17:23 PM:   In file "/opt/build/repo/functions/function-name.js"
3:17:23 PM:   Cannot find module 'follow-redirects'
3:17:23 PM:   Require stack:
3:17:23 PM:   - /opt/buildhome/node-deps/node_modules/@netlify/zip-it-and-ship-it/dist/runtimes/node/bundlers/zisi/resolve.js
gabrielreisn commented 1 year ago

Same here, and it happened in production rather than build time

image

emkis commented 1 year ago

Yeah I had the same problem when trying out using pnpm for serverless functions, then I switched to yarn and everything is working properly now.

Natetronn commented 1 year ago

I just ran into this same issue as well. pnpm worked fine back on Feb 18 at 5:43 PM; that was my last deploy before today and hitting the error this time round; I tried redeploying and clearing cache, but same error.

Netlify has supported pnpm for some time now: https://www.netlify.com/blog/how-to-use-pnpm-with-netlify-build/

The function listed in the error message only has axios as a dependency const axios = require("axios");, so not sure if that has anything to do with it or not.

Natetronn commented 1 year ago

FWIW, I tried adding follow-redirects, to see what would happen, and I get a similar error with another missing dependency; this time from a different function.

error

millsp commented 1 year ago

Hi, I wanted to report that this also started impacting @prisma/client recently:

image

The solution was to add a .npmrc with a setting to flatten the node modules:

node-linker=hoisted

I think this shows that this is a bug in how ZISI handles and resolved pnpm files:

We have a simple reproduction here if you need https://github.com/prisma/ecosystem-tests/tree/dev/platforms-serverless/netlify-github. Before you deploy, remember to delete the .npmrc which contains the workaround mentioned above.

Full logs:

4:26:19 AM: Failed during stage 'building site': Build script returned non-zero exit code: 2 (https://ntl.fyi/exit-code-2)
4:26:15 AM: Netlify Build                                                 
4:26:15 AM: ────────────────────────────────────────────────────────────────
4:26:15 AM: ​
4:26:15 AM: ❯ Version
4:26:15 AM:   @netlify/build 29.20.11
4:26:15 AM: ​
4:26:15 AM: ❯ Flags
4:26:15 AM:   baseRelDir: true
4:26:15 AM:   buildId: -----------------------
4:26:15 AM:   deployId: -----------------------
4:26:15 AM: ​
4:26:15 AM: ❯ Current directory
4:26:15 AM:   /opt/build/repo
4:26:15 AM: ​
4:26:15 AM: ❯ Config file
4:26:15 AM:   /opt/build/repo/netlify.toml
4:26:15 AM: ​
4:26:15 AM: ❯ Context
4:26:15 AM:   branch-deploy
4:26:15 AM: ​
4:26:15 AM: Build command from Netlify app                                
4:26:15 AM: ────────────────────────────────────────────────────────────────
4:26:15 AM: ​
4:26:15 AM: $ PRISMA_TELEMETRY_INFORMATION='ecosystem-tests platform netlify-github build' && pnpm i && pnpm prisma generate
4:26:15 AM: Lockfile is up to date, resolution step is skipped
4:26:15 AM: Already up to date
4:26:15 AM: Done in 459ms
4:26:16 AM: Prisma schema loaded from prisma/schema.prisma
4:26:16 AM: ✔ Generated Prisma Client (v5.3.0-integration-feat-js-connectors-in-client.11) to ./node_modules/.pnpm/@prisma+client@5.3.0-integration-feat-js-connectors-in-client.11_prisma@5.3.0-integration-fea_mhwmcpnbqzp376dw2xayf6cdya/node_modules/@prisma/client in 56ms
4:26:16 AM: Start using Prisma Client in Node.js (See: https://pris.ly/d/client)
4:26:16 AM: ```
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

4:26:16 AM: or start using Prisma Client at the edge (See: https://pris.ly/d/accelerate)
4:26:16 AM: ```
import { PrismaClient } from '@prisma/client/edge'
const prisma = new PrismaClient()

4:26:16 AM: See other ways of importing Prisma Client: http://pris.ly/d/importing-client
4:26:16 AM: ​
4:26:16 AM: (build.command completed in 1.7s)
4:26:16 AM: ​
4:26:16 AM: Functions bundling                                            
4:26:16 AM: ────────────────────────────────────────────────────────────────
4:26:16 AM: ​
4:26:16 AM: Packaging Functions from functions directory:
4:26:16 AM:  - index.js
4:26:16 AM: ​
4:26:16 AM: ​
4:26:16 AM: Dependencies installation error                               
4:26:16 AM: ────────────────────────────────────────────────────────────────
4:26:16 AM: ​
4:26:16 AM:   Error message
4:26:16 AM:   A Netlify Function failed to require one of its dependencies.
4:26:16 AM:   Please make sure it is present in the site's top-level package.json.
​
4:26:16 AM:   In file /opt/build/repo/functions/index.js
4:26:16 AM:   Cannot find module '@prisma/engines-version'
4:26:16 AM:   Require stack:
4:26:16 AM:   - /opt/buildhome/node-deps/node_modules/@netlify/zip-it-and-ship-it/dist/runtimes/node/bundlers/zisi/resolve.js
4:26:16 AM: ​
4:26:16 AM:   Resolved config
4:26:16 AM:   build:
4:26:16 AM:     command: PRISMA_TELEMETRY_INFORMATION='ecosystem-tests platform netlify-github build' && pnpm i && pnpm prisma generate
4:26:16 AM:     commandOrigin: ui
4:26:16 AM:     environment:
4:26:16 AM:       - CI
4:26:16 AM:       - NETLIFY_BETA_PG_URL
4:26:16 AM:       - NETLIFY_BUILD_LIFECYCLE_TRIAL
4:26:16 AM:       - NODE_VERSION
4:26:16 AM:       - PRISMA_TELEMETRY_INFORMATION
4:26:16 AM:     publish: /opt/build/repo
4:26:16 AM:     publishOrigin: default
4:26:16 AM:   functionsDirectory: /opt/build/repo/functions
4:26:18 AM: Build failed due to a user error: Build script returned non-zero exit code: 2
4:26:19 AM: Failing build: Failed to build site
4:26:19 AM: Finished processing build request in 36.298s