@@ -2,14 +2,12 @@ const fs = require('fs')
22const path = require ( 'path' )
33const util = require ( 'util' )
44
5- const nextOnNetlify = require ( 'next-on-netlify' )
6- const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
7- const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
85const findUp = require ( 'find-up' )
96const makeDir = require ( 'make-dir' )
107const { copy } = require ( 'cpx' )
118
129const isStaticExportProject = require ( './helpers/isStaticExportProject' )
10+ const { validateNextUsage } = require ( './helpers/validateNextUsage' )
1311
1412const pWriteFile = util . promisify ( fs . writeFile )
1513const pCopy = util . promisify ( copy )
@@ -22,6 +20,8 @@ module.exports = {
2220 async onPreBuild ( { netlifyConfig, packageJson, utils } ) {
2321 const { failBuild } = utils . build
2422
23+ validateNextUsage ( failBuild )
24+
2525 if ( Object . keys ( packageJson ) . length === 0 ) {
2626 return failBuild ( `Could not find a package.json for this project` )
2727 }
@@ -46,6 +46,11 @@ module.exports = {
4646
4747 const nextConfigPath = await findUp ( 'next.config.js' )
4848 if ( nextConfigPath !== undefined ) {
49+ // We cannot load `next` at the top-level because we validate whether the
50+ // site is using `next` inside `onPreBuild`.
51+ const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
52+ const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
53+
4954 // If the next config exists, fail build if target isnt in acceptableTargets
5055 const acceptableTargets = [ 'serverless' , 'experimental-serverless-trace' ]
5156 const nextConfig = loadConfig ( PHASE_PRODUCTION_BUILD , path . resolve ( '.' ) )
@@ -66,6 +71,11 @@ module.exports = {
6671 } ,
6772 async onBuild ( { constants : { PUBLISH_DIR , FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC } } ) {
6873 console . log ( `** Running Next on Netlify package **` )
74+
75+ // We cannot load `next-on-netlify` (which depends on `next`) at the
76+ // top-level because we validate whether the site is using `next`
77+ // inside `onPreBuild`.
78+ const nextOnNetlify = require ( 'next-on-netlify' )
6979 nextOnNetlify ( )
7080
7181 // Next-on-netlify puts its files into out_functions and out_publish
0 commit comments