Skip to content

Plugin load if next is not available #8

Closed
@ehmicky

Description

@ehmicky

next-on-netlify requires next as a peerDependency:

https://github.com/netlify/next-on-netlify/blob/7d1fd5e08969455035bf9918a6fb2d707984c4a1/package.json#L37-L39

This repository requires next-on-netlify as a production dependency:

https://github.com/netlify/netlify-plugin-nextjs/blob/ef4529016ed0f87ae89d5f312dc8f8f08d1ef7b6/package.json#L24-L29

If a site is using this plugin, but forgot to install next, this plugin will thrown when being require()'d, as opposed to when onPreBuild is run. This results in an error message which might be confusing:

Uncaught Error: Cannot find module 'next/constants'
Require stack:
- /home/ether/Desktop/node_modules/next-on-netlify/lib/helpers/getNextDistDir.js
- /home/ether/Desktop/node_modules/next-on-netlify/lib/config.js
- /home/ether/Desktop/node_modules/next-on-netlify/lib/steps/prepareFolders.js
- /home/ether/Desktop/node_modules/next-on-netlify/index.js
- /home/ether/Desktop/node_modules/@netlify/plugin-nextjs/index.js
- <repl>
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:900:15)
    at Function.Module._load (node:internal/modules/cjs/loader:745:27)
    at Module.require (node:internal/modules/cjs/loader:972:19)
    at require (node:internal/modules/cjs/helpers:88:18) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/ether/Desktop/node_modules/next-on-netlify/lib/helpers/getNextDistDir.js',
    '/home/ether/Desktop/node_modules/next-on-netlify/lib/config.js',
    '/home/ether/Desktop/node_modules/next-on-netlify/lib/steps/prepareFolders.js',
    '/home/ether/Desktop/node_modules/next-on-netlify/index.js',
    '/home/ether/Desktop/node_modules/@netlify/plugin-nextjs/index.js',
    '<repl>'
  ]
}

To fix this:

  • next and next-on-netlify should be require()'d only inside the if (isNextProject) { ... } block
  • _isNextProject() should try to require('next'), and return false if that throws an error

We might want to also wonder: are we ok with this plugin requiring users to install Next.js as a dependency (as opposed to doing it for them)?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions