Unverified Commit ce1a1706 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Improve error message for importing unsupported native modules (#30829)

Improvement for #30696, covering more native module when failed on edge runtime
parent 14841c94
......@@ -90,6 +90,7 @@ import {
printCustomRoutes,
printTreeView,
getCssFilePaths,
getUnresolvedModuleFromError,
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
......@@ -691,12 +692,10 @@ export default async function build(
console.error()
// When using the web runtime, common Node.js native APIs are not available.
if (
hasConcurrentFeatures &&
error.indexOf("Module not found: Can't resolve 'fs'") > -1
) {
const moduleName = getUnresolvedModuleFromError(error)
if (hasConcurrentFeatures && moduleName) {
const err = new Error(
`Native Node.js APIs are not supported in the Edge Runtime with \`concurrentFeatures\` enabled. Found \`fs\` imported.\n\n`
`Native Node.js APIs are not supported in the Edge Runtime with \`concurrentFeatures\` enabled. Found \`${moduleName}\` imported.\n\n`
) as NextError
err.code = 'EDGE_RUNTIME_UNSUPPORTED_API'
throw err
......
import createStore from 'next/dist/compiled/unistore'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { flushAllTraces } from '../../trace'
import { getUnresolvedModuleFromError } from '../utils'
import * as Log from './log'
......@@ -86,12 +87,10 @@ store.subscribe((state) => {
}
}
if (
state.hasServerWeb &&
cleanError.indexOf("Module not found: Can't resolve 'fs'") > -1
) {
const moduleName = getUnresolvedModuleFromError(cleanError)
if (state.hasServerWeb && moduleName) {
console.error(
`Native Node.js APIs are not supported in the Edge Runtime with \`concurrentFeatures\` enabled. Found \`fs\` imported.\n`
`Native Node.js APIs are not supported in the Edge Runtime with \`concurrentFeatures\` enabled. Found \`${moduleName}\` imported.\n`
)
return
}
......
......@@ -36,6 +36,7 @@ import { setHttpAgentOptions } from '../server/config'
import { NextConfigComplete } from '../server/config-shared'
import isError from '../lib/is-error'
const { builtinModules } = require('module')
const fileGzipStats: { [k: string]: Promise<number> | undefined } = {}
const fsStatGzip = (file: string) => {
const cached = fileGzipStats[file]
......@@ -1133,3 +1134,13 @@ export function isFlightPage(
})
return isRscPage
}
export function getUnresolvedModuleFromError(
error: string
): string | undefined {
const moduleErrorRegex = new RegExp(
`Module not found: Can't resolve '(\\w+)'`
)
const [, moduleName] = error.match(moduleErrorRegex) || []
return builtinModules.find((item: string) => item === moduleName)
}
......@@ -19,7 +19,7 @@ import css from './css'
const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')]
const appDir = join(__dirname, '../app')
const fsTestAppDir = join(__dirname, '../app-fs')
const nativeModuleTestAppDir = join(__dirname, '../unsupported-native-module')
const distDir = join(__dirname, '../app/.next')
const documentPage = new File(join(appDir, 'pages/_document.js'))
const appPage = new File(join(appDir, 'pages/_app.js'))
......@@ -89,8 +89,8 @@ describe('concurrentFeatures - basic', () => {
})
it('should warn user that native node APIs are not supported', async () => {
const fsImportedErrorMessage =
'Native Node.js APIs are not supported in the Edge Runtime with `concurrentFeatures` enabled. Found `fs` imported.'
const { stderr } = await nextBuild(fsTestAppDir)
'Native Node.js APIs are not supported in the Edge Runtime with `concurrentFeatures` enabled. Found `dns` imported.'
const { stderr } = await nextBuild(nativeModuleTestAppDir)
expect(stderr).toContain(fsImportedErrorMessage)
})
})
......
let F_OK
let EOF
if (typeof window === 'undefined') {
F_OK = require('fs').constants.F_OK
EOF = require('dns').EOF
}
export default function Index() {
console.log(F_OK)
return 'Access Node.js fs'
console.log(EOF)
return 'Access Node.js native module dns'
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment