Skip to content

Fix require('next') when this plugin is pre-installed #55

Closed
@ehmicky

Description

@ehmicky

In production, we pre-install Build plugins in /opt/buildhome/.netlify-build-plugins. However, the repository is in /opt/build/repo. Therefore, trying to require a dependency of the site (also known as peer dependency) from a plugin does not work. This problem is described in details in this issue.

In the case of both this plugin and next-on-netlify, we are doing require('next'). We are doing this to allow users to choose their own Next.js version instead of forcing one (see #25). This currently fails in production due to the problem above.

The proper solution to this problem is detailed in this issue. However, this is not a quick fix, so we need an alternative in the meantime.

The only solution I can think of is the following: if we detect that the plugin is run in production, use require.resolve() to locate next. In practice this would mean lines like:

const { PHASE_PRODUCTION_BUILD } = require('next/constants')

would be changed to:

const requirePeerDependency = function(modulePath) {
  if (process.env.NETLIFY === 'true') {
    return require(require.resolve(modulePath, { paths: ['.'] } }))
  }

  return require(modulePath)
}

const { PHASE_PRODUCTION_BUILD } = requirePeerDependency('next/constants')

This is hacky. This would need to be done in next-on-netlify too.

What do you think?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions