Next.js Discord

Discord Forum

next.config.js failing type error

Answered
Rampur Greyhound posted this in #help-forum
Open in Discord
Rampur GreyhoundOP
I am running a nextra vercel template (the documentation template), this works on vercel launch but on local host does not

code;
const withNextra = require('nextra')({
theme: 'nextra-theme-docs',
themeConfig: './theme.config.tsx',
})

module.exports = withNextra()

error:
const withNextra = require('nextra')({
^

TypeError: require(...) is not a function


Explain what you've tried to solve the issue:
i've tried running a different pnpm version etc. and trying the next.config.js file from the official nextra docs but all to no avail
Answered by joulev
alright i got it to work.

basically the template repo is extremely out of date. it is only compatible with nextra v2 but the latest inside your package.json is installing nextra v4.

if you want to continue on that template, edit the package.json and replace the latest with 2.13.2 then rerun pnpm i to get the correct dependency version. then it will just work, no need to update next.config.js or its file name.

but i'd suggest you discard the whole template and just follow the documentation for the latest nextra version: https://nextra.site/docs/docs-theme/start
View full answer

21 Replies

@joulev if you have `type: module` in your package.json, remove it. if not: if the file name is `next.config.mjs`, rename it to `next.config.js`.
Rampur GreyhoundOP
mjs threw this in terminal
const withNextra = require('nextra')({
^

ReferenceError: require is not defined in ES module scope, you can use import instead
i'm gessing this is a difference in syntax
let me change the syntax real quick
Rampur GreyhoundOP
yeah it didnt seem tk work
@Rampur Greyhound yeah it didnt seem tk work
so what happened here is: your next.config file is being treated as an esmodule, where require is not allowed – you have to import in esmodules.

this happens if your file name is next.config.mjs, and/or you have "type": "module" in package.json. the above steps i suggested try to make it a cjs file where require is allowed. bit weird that it didn't work.

alternatively you can try using the import syntax
import nextra from "nextra";

const withNextra = nextra({ ... });

const config = withNextra();
export default config;

though whether this works or not depends a lot on the way nextra is bundled.
Rampur GreyhoundOP
nextra-docs-template@0.0.1 dev C:\Users\nithin\IdeaProjects\ftcdesign3
next dev

(node:15100) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of
To eliminate this warning, add "type": "module" to C:\Users\nithin\IdeaProjects\ftcdesign3\package.json.
(Use node --trace-warnings ... to show where the warning was created)
- error [nextra] Error validating nextraConfig
file:///C:/Users/nithin/IdeaProjects/ftcdesign3/node_modules/.pnpm/zod-validation-error@3.4.1_zod@3.24.4/node_modules/zod-validation-error/dist/index.mjs:215
return new ValidationError(message, { cause: zodError });
^

ValidationError [ZodValidationError]: Validation error: Unrecognized key(s) in object: 'theme', 'themeConfig'
at fromZodErrorWithoutRuntimeCheck
at nextra {
details: [
{
code: 'unrecognized_keys',
keys: [ 'theme', 'themeConfig' ],
path: [],
message: "Unrecognized key(s) in object: 'theme', 'themeConfig'"
}
],
[cause]: ZodError: [
{
"code": "unrecognized_keys",
"keys": [
"theme",
"themeConfig"
],
"path": [],
"message": "Unrecognized key(s) in object: 'theme', 'themeConfig'"
}
]
at get error (file:///C:/Users/nithin/IdeaProjects/ftcdesign3/node_modules/.pnpm/zod@3.24.4/node_modules/zod/lib/index.mjs:587:31)
at nextra {
issues: [
{
code: 'unrecognized_keys',
keys: [ 'theme', 'themeConfig' ],
path: [],
message: "Unrecognized key(s) in object: 'theme', 'themeConfig'"
}
],
addIssue: [Function (anonymous)],
addIssues: [Function (anonymous)],
errors: [
{
code: 'unrecognized_keys',
keys: [ 'theme', 'themeConfig' ],
path: [],
message: "Unrecognized key(s) in object: 'theme', 'themeConfig'"
}
]
}
}
@Rampur Greyhound > nextra-docs-template@0.0.1 dev C:\Users\nithin\IdeaProjects\ftcdesign3 > next dev (node:15100) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of To eliminate this warning, add "type": "module" to C:\Users\nithin\IdeaProjects\ftcdesign3\package.json. (Use `node --trace-warnings ...` to show where the warning was created) - error [nextra] Error validating nextraConfig file:///C:/Users/nithin/IdeaProjects/ftcdesign3/node_modules/.pnpm/zod-validation-error@3.4.1_zod@3.24.4/node_modules/zod-validation-error/dist/index.mjs:215 return new ValidationError(message, { cause: zodError }); ^ ValidationError [ZodValidationError]: Validation error: Unrecognized key(s) in object: 'theme', 'themeConfig' at fromZodErrorWithoutRuntimeCheck at nextra { details: [ { code: 'unrecognized_keys', keys: [ 'theme', 'themeConfig' ], path: [], message: "Unrecognized key(s) in object: 'theme', 'themeConfig'" } ], [cause]: ZodError: [ { "code": "unrecognized_keys", "keys": [ "theme", "themeConfig" ], "path": [], "message": "Unrecognized key(s) in object: 'theme', 'themeConfig'" } ] at get error (file:///C:/Users/nithin/IdeaProjects/ftcdesign3/node_modules/.pnpm/zod@3.24.4/node_modules/zod/lib/index.mjs:587:31) at nextra { issues: [ { code: 'unrecognized_keys', keys: [ 'theme', 'themeConfig' ], path: [], message: "Unrecognized key(s) in object: 'theme', 'themeConfig'" } ], addIssue: [Function (anonymous)], addIssues: [Function (anonymous)], errors: [ { code: 'unrecognized_keys', keys: [ 'theme', 'themeConfig' ], path: [], message: "Unrecognized key(s) in object: 'theme', 'themeConfig'" } ] } }
what if you use the code above, and with the file name being next.config.mjs? ref https://github.com/shuding/nextra-docs-template/issues/21#issuecomment-1518559945
@Rampur Greyhound yeah got the same error again
could you show me your current code in a github repository?
Rampur GreyhoundOP
Oh I do see this too though I can edit that and try
@joulev could you show me your current code in a github repository?
Rampur GreyhoundOP
Yeah lemme send the repo one sec
@Rampur Greyhound https://github.com/nithinlook/ftcdesign
alright i got it to work.

basically the template repo is extremely out of date. it is only compatible with nextra v2 but the latest inside your package.json is installing nextra v4.

if you want to continue on that template, edit the package.json and replace the latest with 2.13.2 then rerun pnpm i to get the correct dependency version. then it will just work, no need to update next.config.js or its file name.

but i'd suggest you discard the whole template and just follow the documentation for the latest nextra version: https://nextra.site/docs/docs-theme/start
Answer