Skip to content

Commit

Permalink
Move sharp to optionalDependencies (#18068)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim Neutkens <tim@timneutkens.nl>
  • Loading branch information
styfle and timneutkens committed Oct 21, 2020
1 parent 9dfc9eb commit 80bf81a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
17 changes: 15 additions & 2 deletions errors/install-sharp.md
Expand Up @@ -2,14 +2,27 @@

#### Why This Error Occurred

Using Next.js' built-in Image Optimization requires that you bring your own version of `sharp`.
Using Next.js' built-in Image Optimization requires that you install `sharp`.

Since `sharp` is optional, it may have been skipped if you installed `next` with the [`--no-optional`](https://docs.npmjs.com/cli/install) flag or it may have been skipped if your platform does not support `sharp`.

#### Possible Ways to Fix It

Please install the `sharp` package in your project.
Option 1: Install the `sharp` package in your project.

```bash
npm i sharp
# or
yarn add sharp
```

Option 2: Configure an external loader in `next.config.js` such as [imgix](https://imgix.com).

```js
module.exports = {
images: {
path: 'https://example.com/myaccount/',
loader: 'imgix',
},
}
```
9 changes: 5 additions & 4 deletions packages/next/next-server/server/image-optimizer.ts
Expand Up @@ -229,11 +229,12 @@ export async function imageOptimizer(
sharp = require('sharp')
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
error.message +=
"\nTo use Next.js' built-in Image Optimization, you first need to install `sharp`."
error.message +=
'\nRun `npm i sharp` or `yarn add sharp` inside your workspace.'
error.message += '\n\nLearn more: https://err.sh/next.js/install-sharp'
server.logError(error)
if (upstreamType) {
res.setHeader('Content-Type', upstreamType)
}
res.end(upstreamBuffer)
}
throw error
}
Expand Down
4 changes: 3 additions & 1 deletion packages/next/package.json
Expand Up @@ -125,6 +125,9 @@
"react": "^16.6.0",
"react-dom": "^16.6.0"
},
"optionalDependencies": {
"sharp": "0.26.2"
},
"devDependencies": {
"@next/polyfill-nomodule": "9.5.6-canary.9",
"@taskr/clear": "1.1.0",
Expand Down Expand Up @@ -205,7 +208,6 @@
"resolve": "1.11.0",
"semver": "7.3.2",
"send": "0.17.1",
"sharp": "0.26.2",
"source-map": "0.6.1",
"string-hash": "1.1.3",
"strip-ansi": "6.0.0",
Expand Down

0 comments on commit 80bf81a

Please sign in to comment.