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

reactjs - Error in nextjs app: Error: Cannot find module 'nextdistcompiledws' - Stack Overflow

programmeradmin4浏览0评论

When I try to run npm run dev I get this error:

[Error: Cannot find module 'next/dist/compiled/ws' Require stack:

/mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/dev/hot-reloader-webpack.js

  • /mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/lib/router-utils/setup-dev-bundler.js
  • /mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/lib/router-server.js
  • /mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/lib/start-server.js] { code: 'MODULE_NOT_FOUND', requireStack: [Array] }`

This is my next.config.js file:

const withPWA = require("@ducanh2912/next-pwa").default({
    dest: "public",
    cacheOnFrontEndNav: true,
    aggressiveFrontEndNavCaching: true,
    reloadOnOnline: true,
    swcMinify: true,
    disable: process.env.NODE_ENV === "development",
    workboxOptions: {
        disableDevLogs: true,
    },
    swSrc: "public/serviceWorker.js",
});

/** @type {import('next').NextConfig} */
const nextConfig = {
    typescript: {
        tsconfigPath: "./tsconfig.json",
    },
    images: {
        remotePatterns: [
            {
                protocol: "https",
                hostname: "**.**.**",
            },
        ],
    },

    webpack(config) {
        config.resolve.alias = {
            ...config.resolve.alias,
            "@": require("path").resolve(__dirname),
        };
        return config;
    },
};

module.exports = withPWA(nextConfig);

I'm running my project within WSL on Windows Below are the versions of node js and Next js:

Node: v22.13.1 NextJs: Next.js v15.1.6

I was trying to run my Next.js project, but I encountered a 'MODULE_NOT_FOUND' error related to 'next/dist/compiled/ws'.

To fix this, I tried several solutions:

Added the following configuration in next.config.js:

experimental: {
  outputFileTracingRoot: join(__dirname, '../../'),
}

Cleared the cache using

 rm -rf .next and npm cache clean --force.

Deleted node_modules and package-lock.json, then reinstalled dependencies with

npm install.

But nothing worked

When I try to run npm run dev I get this error:

[Error: Cannot find module 'next/dist/compiled/ws' Require stack:

/mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/dev/hot-reloader-webpack.js

  • /mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/lib/router-utils/setup-dev-bundler.js
  • /mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/lib/router-server.js
  • /mnt/c/Users/Marcus Flavio/OneDrive - Microsoft 365/Área de Trabalho/Projects/smartiming-pwa/node_modules/next/dist/server/lib/start-server.js] { code: 'MODULE_NOT_FOUND', requireStack: [Array] }`

This is my next.config.js file:

const withPWA = require("@ducanh2912/next-pwa").default({
    dest: "public",
    cacheOnFrontEndNav: true,
    aggressiveFrontEndNavCaching: true,
    reloadOnOnline: true,
    swcMinify: true,
    disable: process.env.NODE_ENV === "development",
    workboxOptions: {
        disableDevLogs: true,
    },
    swSrc: "public/serviceWorker.js",
});

/** @type {import('next').NextConfig} */
const nextConfig = {
    typescript: {
        tsconfigPath: "./tsconfig.json",
    },
    images: {
        remotePatterns: [
            {
                protocol: "https",
                hostname: "**.**.**",
            },
        ],
    },

    webpack(config) {
        config.resolve.alias = {
            ...config.resolve.alias,
            "@": require("path").resolve(__dirname),
        };
        return config;
    },
};

module.exports = withPWA(nextConfig);

I'm running my project within WSL on Windows Below are the versions of node js and Next js:

Node: v22.13.1 NextJs: Next.js v15.1.6

I was trying to run my Next.js project, but I encountered a 'MODULE_NOT_FOUND' error related to 'next/dist/compiled/ws'.

To fix this, I tried several solutions:

Added the following configuration in next.config.js:

experimental: {
  outputFileTracingRoot: join(__dirname, '../../'),
}

Cleared the cache using

 rm -rf .next and npm cache clean --force.

Deleted node_modules and package-lock.json, then reinstalled dependencies with

npm install.

But nothing worked

Share Improve this question asked 5 hours ago Marcus-FlavioMarcus-Flavio 211 silver badge2 bronze badges New contributor Marcus-Flavio is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • Have you tried removing package-lock.json and node_modules and run npm i? – Trusha Jadeja Commented 5 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

Just try all of these one by one :-

Try forcing a clean reinstall:

rm -rf node_modules package-lock.json .next
npm cache clean --force
npm install --legacy-peer-deps
npm rebuild
npm run dev

OR Manually install ws :

npm install ws --save-dev

I think there may be some nextjs version issue you are using so try to downgrade or upgrade :

OR Downgrade to Next.js 14 (stable)

npm install next@14
npm run dev

OR Upgrade Next.js

npm install next@latest
npm run dev
发布评论

评论列表(0)

  1. 暂无评论