next.config.js failing type error
Answered
Rampur Greyhound posted this in #help-forum
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
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
if you want to continue on that template, edit the package.json and replace the
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
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
21 Replies
@Rampur Greyhound 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
if you have
if the file name is
type: module
in your package.json, remove it. if not:if the file name is
next.config.mjs
, rename it to next.config.js
.you can also try simply renaming the file to
next.config.cjs
(with the extension cjs
), though i'm not 100% sure whether this works or not@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
no its js currently
@joulev you can also try simply renaming the file to `next.config.cjs` (with the extension `cjs`), though i'm not 100% sure whether this works or not
Rampur GreyhoundOP
this one worked but the page threw a 404 xD
@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
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
this happens if your file name is
alternatively you can try using the
though whether this works or not depends a lot on the way nextra is bundled.
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
syntaximport 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'"
}
]
}
}
also if it helps I am using this repository
https://github.com/shuding/nextra-docs-template
https://github.com/shuding/nextra-docs-template
@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@joulev 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 GreyhoundOP
yeah got the same error again
@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
if you want to continue on that template, edit the package.json and replace the
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
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
@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>
Rampur GreyhoundOP
ahhh okay I see, I initially had some issues deploying to vercel with the standard installation documentation but I'll try that out again in the future
thank you so much!!