最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error Couldn't find a `pages` directory. Please create one under the project root" - Stack Overflo

programmeradmin7浏览0评论

Previously my project setup was

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

This was working fine but I changed the structure to following

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

now this one is not working. I am getting error Couldn't find apagesdirectory. Please create one under the project root

Here is my updated next.config.js

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

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

for absolute import(import Button from 'ponents/Button')

jsconfig.json

{
    "pilerOptions": {
      "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-ponents",
        {
          "ssr": true,
          "displayName": true,
          "preprocess": false
        }
      ]
    ],
    "presets": [
      "next/babel"
    ]
  }

Previously my project setup was

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

This was working fine but I changed the structure to following

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

now this one is not working. I am getting error Couldn't find apagesdirectory. Please create one under the project root

Here is my updated next.config.js

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

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

for absolute import(import Button from 'ponents/Button')

jsconfig.json

{
    "pilerOptions": {
      "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-ponents",
        {
          "ssr": true,
          "displayName": true,
          "preprocess": false
        }
      ]
    ],
    "presets": [
      "next/babel"
    ]
  }
Share Improve this question edited May 20, 2020 at 2:34 Serenity asked May 20, 2020 at 2:15 SerenitySerenity 4,0548 gold badges49 silver badges96 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

As per the official NextJs documentation pages folder can be moved inside the src folder. But Config files like next.config.js and tsconfig.json should be inside the root directory, moving them to src won't work. Same goes for the public directory.

Move your next.config.js file to root folder.

Reference: https://nextjs/docs/advanced-features/src-directory

I managed to get the same error on a Windows system where my pages folder was called "Pages" (capital P). Renaming it to lowercase P fixed it.

发布评论

评论列表(0)

  1. 暂无评论