【问题标题】:Error Couldn't find a `pages` directory. Please create one under the project root"错误找不到“页面”目录。请在项目根目录下创建一个”
【发布时间】:2020-09-06 06:42:33
【问题描述】:

以前我的项目设置是

public
.next
src
   pages
   components
   assets
next.config.js

这工作正常,但我将结构更改为以下

public
src
   client
    next.config.js
    jsconfig.json
    pages
    components
    assets
   server
    index.js

现在这个不工作了。我收到错误Couldn't find apagesdirectory. Please create one under the project root

这是我更新的 next.config.js

const withPlugins = require("next-compose-plugins");
const optimizedImages = require("next-optimized-images");
const withPWA = require("next-pwa");

module.exports = withPlugins(
    [optimizedImages],
    [
      withPWA({
        pwa: {
          dest: "public",
        },
      }),
    ],
    {
      distDir: '../../dist/client',
    }
);

对于绝对导入(从“组件/按钮”导入按钮)

jsconfig.json

{
    "compilerOptions": {
      "baseUrl": "client/"
    }
  }

package.json

"scripts": {
    "dev:client": "nextjs src/client",
    "dev:server": "node src/server --out-dir dist/server --source-maps --watch",
    "dev": "dotenv -e .env.development yarn dev:client & yarn dev:server",
    "build": "yarn build:client && yarn build:server",
    "build:client": "dotenv -e .env.production next build src/client",
    "build:server": "node src/server --out-dir dist/server --source-maps",
    "build:staging": "yarn build:staging:client && yarn build:server",
    "build:staging:client": "dotenv -e .env.staging next build src/client",
    "start": "next start",
  },

.babelrc

{

    "plugins": [
      [
        "styled-components",
        {
          "ssr": true,
          "displayName": true,
          "preprocess": false
        }
      ]
    ],
    "presets": [
      "next/babel"
    ]
  }

【问题讨论】:

    标签: javascript reactjs next.js


    【解决方案1】:

    根据官方 NextJs 文档页面文件夹可以移动到 src 文件夹中。但是像 next.config.js 和 tsconfig.json 这样的配置文件应该在根目录中,将它们移动到 src 是行不通的。公共目录也是如此。

    将 next.config.js 文件移动到根文件夹。

    参考:https://nextjs.org/docs/advanced-features/src-directory

    【讨论】:

    • 现在我将 next.config.js 和 jsconfig.json 移到 src 文件夹之外,即在根目录上,但仍然无法正常工作。
    • 我什至在运行服务器时指向 src/client,您可以在我的脚本中看到这一点
    • 您的 pages 文件夹是在 client 文件夹还是 src 文件夹中?
    • 它在客户端文件夹中。
    • 它应该在 src 文件夹中。 Pages 文件夹应该是 src 文件夹的直接子文件夹。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2015-02-28
    相关资源
    最近更新 更多