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 { ...@@ -90,6 +90,7 @@ import {
printCustomRoutes, printCustomRoutes,
printTreeView, printTreeView,
getCssFilePaths, getCssFilePaths,
getUnresolvedModuleFromError,
} from './utils' } from './utils'
import getBaseWebpackConfig from './webpack-config' import getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin' import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
...@@ -691,12 +692,10 @@ export default async function build( ...@@ -691,12 +692,10 @@ export default async function build(
console.error() console.error()
// When using the web runtime, common Node.js native APIs are not available. // When using the web runtime, common Node.js native APIs are not available.
if ( const moduleName = getUnresolvedModuleFromError(error)
hasConcurrentFeatures && if (hasConcurrentFeatures && moduleName) {
error.indexOf("Module not found: Can't resolve 'fs'") > -1
) {
const err = new Error( 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 ) as NextError
err.code = 'EDGE_RUNTIME_UNSUPPORTED_API' err.code = 'EDGE_RUNTIME_UNSUPPORTED_API'
throw err throw err
......
import createStore from 'next/dist/compiled/unistore' import createStore from 'next/dist/compiled/unistore'
import stripAnsi from 'next/dist/compiled/strip-ansi' import stripAnsi from 'next/dist/compiled/strip-ansi'
import { flushAllTraces } from '../../trace' import { flushAllTraces } from '../../trace'
import { getUnresolvedModuleFromError } from '../utils'
import * as Log from './log' import * as Log from './log'
...@@ -86,12 +87,10 @@ store.subscribe((state) => { ...@@ -86,12 +87,10 @@ store.subscribe((state) => {
} }
} }
if ( const moduleName = getUnresolvedModuleFromError(cleanError)
state.hasServerWeb && if (state.hasServerWeb && moduleName) {
cleanError.indexOf("Module not found: Can't resolve 'fs'") > -1
) {
console.error( 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 return
} }
......
...@@ -36,6 +36,7 @@ import { setHttpAgentOptions } from '../server/config' ...@@ -36,6 +36,7 @@ import { setHttpAgentOptions } from '../server/config'
import { NextConfigComplete } from '../server/config-shared' import { NextConfigComplete } from '../server/config-shared'
import isError from '../lib/is-error' import isError from '../lib/is-error'
const { builtinModules } = require('module')
const fileGzipStats: { [k: string]: Promise<number> | undefined } = {} const fileGzipStats: { [k: string]: Promise<number> | undefined } = {}
const fsStatGzip = (file: string) => { const fsStatGzip = (file: string) => {
const cached = fileGzipStats[file] const cached = fileGzipStats[file]
...@@ -1133,3 +1134,13 @@ export function isFlightPage( ...@@ -1133,3 +1134,13 @@ export function isFlightPage(
}) })
return isRscPage 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' ...@@ -19,7 +19,7 @@ import css from './css'
const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')] const nodeArgs = ['-r', join(__dirname, '../../react-18/test/require-hook.js')]
const appDir = join(__dirname, '../app') 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 distDir = join(__dirname, '../app/.next')
const documentPage = new File(join(appDir, 'pages/_document.js')) const documentPage = new File(join(appDir, 'pages/_document.js'))
const appPage = new File(join(appDir, 'pages/_app.js')) const appPage = new File(join(appDir, 'pages/_app.js'))
...@@ -89,8 +89,8 @@ describe('concurrentFeatures - basic', () => { ...@@ -89,8 +89,8 @@ describe('concurrentFeatures - basic', () => {
}) })
it('should warn user that native node APIs are not supported', async () => { it('should warn user that native node APIs are not supported', async () => {
const fsImportedErrorMessage = const fsImportedErrorMessage =
'Native Node.js APIs are not supported in the Edge Runtime with `concurrentFeatures` enabled. Found `fs` imported.' 'Native Node.js APIs are not supported in the Edge Runtime with `concurrentFeatures` enabled. Found `dns` imported.'
const { stderr } = await nextBuild(fsTestAppDir) const { stderr } = await nextBuild(nativeModuleTestAppDir)
expect(stderr).toContain(fsImportedErrorMessage) expect(stderr).toContain(fsImportedErrorMessage)
}) })
}) })
......
let F_OK let EOF
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
F_OK = require('fs').constants.F_OK EOF = require('dns').EOF
} }
export default function Index() { export default function Index() {
console.log(F_OK) console.log(EOF)
return 'Access Node.js fs' 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