gpt4 book ai didi

javascript - 找不到 `pages` 目录。带有 pkg​​ 的 NextJS

转载 作者:行者123 更新时间:2023-12-05 06:49:18 26 4
gpt4 key购买 nike

我想为 Windows 制作一个 NextJS 可执行应用程序,但我收到了这个恼人的错误:

(node:18500) UnhandledPromiseRejectionWarning: Error: > Couldn't find a pages directory. Please create one under the project root

我在根文件夹中有一个 pages 文件夹

下面是server.js

const { createServer } = require('http')
const next = require('next')

const port = parseInt(process.env.PORT, 10) || 3003
const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev, dir: __dirname })
const handle = app.getRequestHandler()

app.prepare().then(() => {
createServer((req, res) => {
const parsedUrl = new URL(req.url, 'http://w.w')
const { pathname, query } = parsedUrl

if (pathname === '/a') {
app.render(req, res, '/a', query)
} else if (pathname === '/b') {
app.render(req, res, '/b', query)
} else {
handle(req, res, parsedUrl)
}
}).listen(port, (err) => {
if (err) throw err
console.log(`> Ready on http://localhost:${port}`)
})
})

package.json

{
"name": "custom-server",
"version": "1.0.0",
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "cross-env NODE_ENV=production node server.js",
"pkg": "pkg . --targets node12-win-x64 --out-path pkg"
},
"dependencies": {
"cross-env": "^7.0.2",
"next": "latest",
"pkg": "^4.4.9",
"react": "^17.0.1",
"react-dom": "^17.0.1"
},
"main": "server.js",
"license": "MIT",
"bin": "server.js",
"pkg": {
"assets": [
".next/**/*"
],
"scripts": [
".next/server/**/*.js"
]
}
}

我怎样才能解决这个问题并使 NextJS 工作可执行?

最佳答案

当您运行构建时,请确保将 NODE_ENV 设置为生产环境,并根据文档将 conf 传递给您的 server.js。

const app = next({ dev, dir: __dirname, conf : {output : "standalone"} })

除了运行开发之外,NODE_ENV 应该是您所有任务的生产环境。

关于javascript - 找不到 `pages` 目录。带有 pkg​​ 的 NextJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66613694/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com