Friday, 13 August 2021

Next.js Scripts Error: Cannot find module '../../webpack-runtime.js'

I want to create RSS script using Next.js.

So I put up a script in a subfolder inside the root folder scripts/ & named it build-rss.js

next.config.js

module.exports = {
    webpack: (config, options) => {
        config.module.rules.push({
            test: /\.svg$/,
            issuer: { and: [/\.(js|ts|md)x?$/] },
            use: [
                {
                    loader: '@svgr/webpack',
                    options: {
                        prettier: false,
                        svgo: true,
                        svgoConfig: { plugins: [{ removeViewBox: false }] },
                        titleProp: true,
                    },
                },
            ],
        })

        if (!options.dev && options.isServer) {
            const originalEntry = config.entry

            config.entry = async () => {
                const entries = { ...(await originalEntry()) }
                entries['./scripts/build-rss'] = './scripts/build-rss.js'
                return entries
            }
        }

        if (!options.isServer) {
            config.resolve.fallback.fs = false
        }

        return config
    },
}

When I try to run my script npm run build:development which in package.json represents:

"scripts": {
    "clean": "rimraf .next",
    "dev": "next dev",
    "export": "next export",
    "start": "next start",
    "lint": "next lint",
    "build:development": "next build && npm run export && npm run rss:development",
    "build": "next build && npm run export && npm run rss",
    "rss:development": "node ./.next/server/scripts/build-rss.js",
    "rss": "node ./.next/serverless/scripts/build-rss.js"
}

It throws an error saying:

Error: Cannot find module '../../webpack-runtime.js'

But I checked. The file does exist.

The blunder is this used to work earlier. Probably few versions ago when my other project used the same combination.

I have made a complete reproduction showcasing the error → https://github.com/deadcoder0904/next-script-rss-error

Just clone it, install it & try the script npm run build:development in the terminal to see the error.



from Next.js Scripts Error: Cannot find module '../../webpack-runtime.js'

No comments:

Post a Comment