Next.js Discord

Discord Forum

Using @next/bundle-analyzer with App router and "type: module"

Unanswered
Ocicat posted this in #help-forum
Open in Discord
OcicatOP
I'm trying to use @next/bundle-analyzer with app router and when I try to run it, it fails with the following reason:

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/theo.despoudis/Workspace/faustjs/examples/next/app-router/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.


My next.config.js is :

/** @type {import('next').NextConfig} */
const config = {
  appDir: true,
  experimental: {
    serverActions: true,
  },
};


const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: true,
})
module.exports = withBundleAnalyzer(config)


package.json

{
  "name": "@faustwp/app-router-example",
  "private": true,
  "type": "module",
  ...
}


How can I use this plugin with this configuration? I tried to rename the next.config.cjs with no avail.

8 Replies

Giant panda
Once again all your shared snippets are irrelevant. You have to show the actual code that causes this issue.
OcicatOP
code is here>
https://github.com/wpengine/faustjs/tree/canary/examples/next/app-router

I just tried to add the @next/bundle-analyzer in next.config.js
Giant panda
Oh sorry, that is an entirely different error compared to the one you posted into the general channel...
Just follow what the warning tells you and change the file type
OcicatOP
Doesn't work. It does not produce the bundle analyse page. It skips it.
Giant panda
And how did you confirm whether the page is not being generated?
OcicatOP
I don't see any files generated in the .next folder and also when I visit the app it does not print the bundle analysis pages. When I was using this on the pages on the other hand it was working as expected.
I can see the .next/analyze folder in the pages version but not in the app-router version