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

[Bug]: IPX Error: Cannot find module '../build/Release/sharp-linux-x64.node'

Open joernb opened this issue 2 years ago • 2 comments

Summary

I deployed a minimalistic Next.js starter that uses next/image and expected the Next.js image optimization to work without any errors. However, requests to the IPX function fail with a 500 error and the IPX function log shows this:

Something went wrong installing the "sharp" module
Cannot find module '../build/Release/sharp-linux-x64.node'

[...]

Possible solutions:
- Install with the --verbose flag and look for errors: "npm install --ignore-scripts=false --verbose sharp"
- Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install

The linked documentation says:

The architecture and platform of Node.js used for npm install must be the same as the architecture and platform of Node.js used at runtime.

Not sure if this is the root cause, but I ran npm install as well as netlify deploy --build --prod on macOS. The deployed IPX function that complains about some missing binaries seems to run on Linux.

Steps to reproduce

On macOS:

  1. npx create-next-app
  2. Add an image to a Next.js page:
import Image from "next/image";
import imgPath from "../public/image.jpg";

export default function Home() {
  return (
    <div>
      <Image alt="" src={imgPath} width={3024} height={4032} />
    </div>
  );
}
  1. Follow README instructions at https://github.com/netlify/netlify-plugin-nextjs to add plugin and netlify.toml.
  2. netlify deploy --build --prod
  3. Open deployed site

A link to a reproduction repository

https://github.com/joernb/next-netlify-test

Plugin version

4.3.2

More information about your build

  • [X] I am building using the CLI
  • [X] I am building using file-based configuration (netlify.toml)

What OS are you using?

Mac OS

Your netlify.toml file

`netlify.toml`
[build]
  command = "npm run build"
  publish = ".next"

[[plugins]]
  package = "@netlify/plugin-nextjs"

Your public/_redirects file

`_redirects`
# Paste content of your `_redirects` file here

Your next.config.js file

`next.config.js`
# Paste content of your `next.config.js` file here. Check there is no private info in there.

Builds logs (or link to your logs)

Build logs
# Paste logs here

Function logs

Function logs

_ipx log:

Apr 5, 11:44:19 PM: fcd1b3ff ERROR  Error: 
Something went wrong installing the "sharp" module

Cannot find module '../build/Release/sharp-linux-x64.node'
Require stack:
- /var/task/node_modules/sharp/lib/sharp.js
- /var/task/node_modules/sharp/lib/constructor.js
- /var/task/node_modules/sharp/lib/index.js

Possible solutions:
- Install with the --verbose flag and look for errors: "npm install --ignore-scripts=false --verbose sharp"
- Install for the current linux-x64 runtime: "npm install --platform=linux --arch=x64 sharp"
- Consult the installation documentation: https://sharp.pixelplumbing.com/install
    at Object.<anonymous> (/var/task/node_modules/sharp/lib/sharp.js:31:9)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:101:18)
    at Object.<anonymous> (/var/task/node_modules/sharp/lib/constructor.js:8:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)

.next JSON files

generated .next JSON files
# Paste file contents here. Please check there isn't any private info in them
# You can either build locally, or download the deploy from Netlify by clicking the arrow next to the deploy time.

joernb avatar Apr 05 '22 22:04 joernb

Hi @joernb Yes, you identified the issue correctly. If you deploy from Mac OS, sharp will install the Mac binaries instead of Linux, which is the environment where the site runs in production. If you remove your node_modules and package-lock, then run the flags npm install--platform=linux --arch=x64 then it may work, but I haven't tested it. ALternatively you can build the site on Netlify, which uses Linux.

ascorbic avatar Apr 06 '22 07:04 ascorbic

Thanks @ascorbic. Deploying after npm install --platform=linux --arch=x64 actually works.

However having to do this just because of one internal IPX dependency and Netlify's internal choice to run their functions on Linux X64 feels like a leaky abstraction to me. Wouldn't it be better to solve this internally somehow? I was expecting Netlify's tooling to hide those things away so that I do not have to care about the execution environment.

joernb avatar Apr 06 '22 18:04 joernb

Closing due to inactivity/a workaround was found, but the team will be taking your feedback back for further discussion @joernb

ericapisani avatar Oct 12 '22 15:10 ericapisani