Skip to content

[Bug?]: cannot load yarn.config.js when package.json has type: module #5720

@evan-thrive-health

Description

@evan-thrive-health

Self-service

  • I'd be willing to implement a fix

Describe the bug

When package.json contains the directive "type": "module", yarn constraints fails with the following error message.

yarn.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename yarn.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/edickinson/Apps/constraint-repro/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

If I rename the file to yarn.config.cjs then the file isn't imported by yarn. And changing type to commonjs isn't viable for our project.

Additionally, converting yarn.config.js to use ES Modules (as in export async function constraints() {}) fails with the error Internal Error: require() of ES Module /Users/edickinson/Apps/constraint-repro/yarn.config.js from /Users/edickinson/Apps/constraint-repro/.yarn/releases/yarn-4.0.0-rc.50.cjs not supported.

There's a previous comment that proposes changing the config file to yarn.config.cjs, but maybe that work fell through the cracks? Seems like that would be a good resolution to the issue

To reproduce

package.json

{
  "name": "constraint-repro",
  "packageManager": "[email protected]",
  "type": "module"
}

yarn.config.js

module.exports = {
    async constraints({Yarn}) {
      for (const dep of Yarn.dependencies({ ident: 'react' })) {
        dep.update(`18.0.0`);
      }
    },
  };

Environment

System:
    OS: macOS 13.5.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 18.16.1 - /private/var/folders/dn/r5w78tm5375c0sj8_wb3wmn80000gn/T/xfs-5fdca915/node
    Yarn: 4.0.0-rc.50 - /private/var/folders/dn/r5w78tm5375c0sj8_wb3wmn80000gn/T/xfs-5fdca915/yarn
    npm: 9.5.1 - /usr/local/bin/npm

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions