Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nextjs server crashes a esm package with type="module" #61430

Open
jantimon opened this issue Jan 31, 2024 · 1 comment
Open

nextjs server crashes a esm package with type="module" #61430

jantimon opened this issue Jan 31, 2024 · 1 comment
Labels
bug Issue was opened via the bug report template.

Comments

@jantimon
Copy link
Contributor

jantimon commented Jan 31, 2024

Link to the code that reproduces this issue

https://github.com/jantimon/reproduction-webpack-import-module-bug/tree/nextjs

To Reproduce

  1. clone the reproduction:
    git clone https://github.com/jantimon/reproduction-webpack-import-module-bug.git
  2. switch to the nextjs branch
    git checkout nextjs
  3. npm i
  4. npm run dev

I am using the Webpack loader API this.importModule which invokes vm.runInThisContext to execute the module code.

However, as soon as the package.json file includes "type": "module" vm.runInThisContext does not have access to require and module anymore:

./banner.ts

import cowsay from "cowsay";

console.log(cowsay.say({
    text : "I'm a moooodule",
    e : "oO",
    T : "U "
}));

export const banner =  `;console.log("Build at ${new Date()}");\n`

loader.cjs

module.exports = function (source) {
    const callback = this.async();
    this.importModule('./banner.ts', {
        layer: "banner-loader"
    }, function (err, result) {
        callback(err, !err && (result?.banner || "") + source);
    });
}

next.config.mjs

/** @type {import('next').NextConfig} */
const nextConfig = {
    webpack: (config) => {
        config.module.rules.push({
                test: /\.tsx?$/,
                use: './loader.cjs',
                issuerLayer: (issuer) => issuer !== "banner-loader" ? issuer : "",
                exclude: /node_modules/,
        });
        return config;
    },
};
export default nextConfig;

next generates the following code: module.exports = require("cowsay"); which does not work in CSM and therefore breaks:

ReferenceError: require is not defined Generated code for external commonjs "cowsay" 1 | module. exports = require("cowsay");

Current vs. Expected behavior

it would be nice if adding "type" = "module" when using the Webpack loader API this.importModule would not crash nextjs

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.1.0: Mon Oct  9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000
Binaries:
  Node: 20.9.0
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: 8.9.2
Relevant Packages:
  next: 14.1.0
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.3.3

Which area(s) are affected? (Select all that apply)

Not sure

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local)

Additional context

According to @alexander-akait it is not a webpack issue: webpack/webpack#18023

@jantimon jantimon added the bug Issue was opened via the bug report template. label Jan 31, 2024
@VasiliyTelyatnikovTakeda

I faced with a similar issue. We use internal UI library lives in the same monorepository when our Next.js application exist and connected via workspace:* feature. When we switched to "type": "module" in the library's package.json, which is our requirement to ship the UI library outside of the current monorepository, our Next.js application build has broken (but building itself has been done successfully).

Do we have some workaround for this issue? It completely brakes our websites.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

2 participants