英文:
Error: ENOENT: no such file or directory, scandir 'pages'
问题
我正在尝试在我的Next.js应用中添加站点地图,但当我访问`http://localhost:3000/sitemap.xml`页面时,它抛出以下错误消息:
错误:ENOENT:没有这样的文件或目录,扫描'directory'页面
pages/sitemap.xml.js文件代码:
import * as fs from "fs"
import { getDocuments } from "../firebase/firestore"
const Sitemap = () => {
return null
}
export const getServerSideProps = async ({ res }) => {
const BASE_URL = "https://www.example.com"
const staticPaths = fs
.readdirSync("pages")
.filter((staticPage) => {
return !["sitemap.xml.js", "404.js", "_app.js", "_document.js", "api"].includes(
staticPage
)
})
.map((staticPagePath) => {
return `${BASE_URL}/${staticPagePath.replace(".js", "")}`
})
const collectionOne = await getDocuments("collection-one")
const collectionTwo = await getDocuments("collection-two")
const collectionOnePaths = collectionOne.map((singleDocument) => {
return `${BASE_URL}/collection-one/${singleDocument.id}`
})
const collectionTwoPaths = collectionTwo.map((singleSolutions) => {
return `${BASE_URL}/collection-two/${singleSolutions.id}`
})
const allPaths = [...staticPaths, ...collectionOnePaths, ...collectionTwoPaths]
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${allPaths
.map((url) => {
return `
<url>
<loc>${url}</loc>
</url>
`
})
.join("")}
</urlset>
`
res.setHeader("Content-Type", "text/xml")
res.write(sitemap)
res.end()
return {
props: {},
}
}
export default Sitemap
请有人帮我解决这个问题?
<details>
<summary>英文:</summary>
I'm trying to add a sitemap in my nextjs app but when I visit the `http://localhost:3000/sitemap.xml` page it throws this error message:
Error: ENOENT: no such file or directory, scandir 'pages'
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/cxuvB.png
pages/sitemap.xml.js file code:
import * as fs from "fs"
import { getDocuments } from "../firebase/firestore"
const Sitemap = () => {
return null
}
export const getServerSideProps = async ({ res }) => {
const BASE_URL = "https://www.example.com"
const staticPaths = fs
.readdirSync("pages")
.filter((staticPage) => {
return !["sitemap.xml.js", "404.js", "_app.js", "_document.js", "api"].includes(
staticPage
)
})
.map((staticPagePath) => {
return ${BASE_URL}/${staticPagePath.replace(".js", "")}
})
const collectionOne = await getDocuments("collection-one")
const collectionTwo = await getDocuments("collection-two")
const collectionOnePaths = collectionOne.map((singleDocument) => {
return ${BASE_URL}/collection-one/${singleDocument.id}
})
const collectionTwoPaths = collectionTwo.map((singleSolutions) => {
return ${BASE_URL}/collection-two/${singleSolutions.id}
})
const allPaths = [...staticPaths, ...collectionOnePaths, ...collectionTwoPaths]
const sitemap = <?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${allPaths
.map((url) => {
return
<url>
<loc>${url}</loc>
</url>
})
.join("")}
</urlset>
res.setHeader("Content-Type", "text/xml")
res.write(sitemap)
res.end()
return {
props: {},
}
}
export default Sitemap
Anyone, please help me with this issue?
</details>
# 答案1
**得分**: 1
当您运行`npm run dev`命令启动您的Next.js应用时,工作目录被设置为**您的应用程序的根目录**,该目录可能不包含*pages目录*。
您可以使用`process.cwd()`方法获取应用程序的当前工作目录,然后将pages目录追加到路径中。
例如,在`readdirSync`中将`"page"`设置为`pagesDir = path.join(process.cwd(), "pages");`
```javascript
const pagesDir = path.join(process.cwd(), "pages");
const staticPaths = fs
.readdirSync(pagesDir)
.filter((staticPage) => {
return !["sitemap.xml.js", "404.js", "_app.js", "_document.js", "api"].includes(
staticPage
);
})
.map((staticPagePath) => {
return `${BASE_URL}/${staticPagePath.replace(".js", "")}`
})
如果不起作用,请尝试以下方法:
const path = require('path');
const fs = require('fs');
const files = fs.readdirSync(path.resolve(__dirname, 'pages'));
英文:
when you run the npm run dev
command to start your Next.js app, the working directory is set to the root directory of your app, which may not contain the pages directory.
you can use the process.cwd()
method to get the current working directory of your app, and then append the pages directory to the path
For example set "page"
in readdirSync
to pagesDir = path.join(process.cwd(), "pages");
const pagesDir = path.join(process.cwd(), "pages");
const staticPaths = fs
.readdirSync(pagesDir)
.filter((staticPage) => {
return !["sitemap.xml.js", "404.js", "_app.js", "_document.js", "api"].includes(
staticPage
)
})
.map((staticPagePath) => {
return `${BASE_URL}/${staticPagePath.replace(".js", "")}`
})
if it doesn't work try it with:
const path = require('path');
const fs = require('fs');
const files = fs.readdirSync(path.resolve(__dirname, 'pages'));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论