diff --git a/.changeset/cyan-points-fail.md b/.changeset/cyan-points-fail.md new file mode 100644 index 0000000000..951644bf96 --- /dev/null +++ b/.changeset/cyan-points-fail.md @@ -0,0 +1,5 @@ +--- +'nextra': patch +--- + +remove warning `Watchpack Error (initial scan): Error: ENOTDIR: not a directory, scandir 'path-to-your-node_modules/next/dist/pages/_app.js'` diff --git a/packages/nextra/src/server/index.ts b/packages/nextra/src/server/index.ts index ba4b8ad553..e7005dded5 100644 --- a/packages/nextra/src/server/index.ts +++ b/packages/nextra/src/server/index.ts @@ -1,6 +1,5 @@ /* eslint-env node */ -import { createRequire } from 'node:module' -import { sep } from 'node:path' +import { join, sep } from 'node:path' import type { NextConfig } from 'next' import { fromZodError } from 'zod-validation-error' import type { Nextra } from '../types' @@ -19,8 +18,6 @@ import { NextraPlugin, NextraSearchPlugin } from './webpack-plugins/index.js' const DEFAULT_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] -const require = createRequire(import.meta.url) - const AGNOSTIC_PAGE_MAP_PATH = `.next${sep}static${sep}chunks${sep}nextra-page-map` const nextra: Nextra = nextraConfig => { @@ -124,14 +121,20 @@ const nextra: Nextra = nextraConfig => { ) } } - - const defaultESMAppPath = require.resolve('next/dist/esm/pages/_app.js') - const defaultCJSAppPath = require.resolve('next/dist/pages/_app.js') - - config.resolve.alias = { - ...config.resolve.alias, - // Resolves ESM _app file instead cjs, so we could import theme.config via `import` statement - [defaultCJSAppPath]: defaultESMAppPath + config.resolve.alias = { ...config.resolve.alias } + const { alias } = config.resolve + + const appAlias = alias['private-next-pages/_app'] + const appESM = 'next/dist/esm/pages/_app' + if (appAlias) { + alias['private-next-pages/_app'] = [ + // Cut last element which points to CJS _app file + ...appAlias.slice(0, -1), + // Resolves ESM _app file instead CJS, so we could import `theme.config` via `import` statement + appESM + ] + } else { + alias[join(alias.next, 'dist', 'pages', '_app')] = appESM } const rules = config.module.rules as RuleSetRule[]