Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in nextjs app: Error: Cannot find module 'next/dist/compiled/ws'

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

like image 915
Marcus-Flavio Avatar asked Nov 22 '25 21:11

Marcus-Flavio


1 Answers

was experiencing the same issue ("Cannot find module 'next/dist/compiled/ws'").

Updating my Node.js version to v20.19.1 completely solved the problem for me.

If you're encountering this error, definitely check your Node.js version and consider changing it.

like image 80
DervisEfeK Avatar answered Nov 24 '25 16:11

DervisEfeK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!