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.
.nextdirectory is not present in your repo. also, that filewebpack-runtime.jsis being located in.next/serverlessdirectory. when you're running the script, tryprocess.cwd()to know the context directory from where script is being executed..nextgets created when you runnext build& I have ignored it specifically in.gitignoreso its not in the repo but exists locally. It only gets on.next/serverlesson Vercel but on development it generates on.next/server. I'll try yourprocess.cwd()suggestion but would love to know if you have anymore suggestions :)process.cwd()but it throws error before even runningbuild-rss.js. Notice, how the entire file is empty & it still doesn't work. I did try with content in the file but nope it doesn't work either.nextjs, but usually this issue is because how you call the file, and how does it's directory context change. mostly either it's looking in the wrong directory, or the required file is being generated after the script has finished