Skip to content

Cannot use the router plugin in commonjs webpack config #3186

Closed

Description

@paypal-edveriksson

Which project does this relate to?

Router

Describe the bug

This is the error that happens. chalk v5 does not provide support for commonjs. Since the router plugin package do provide commonjs exports I think this is a bug that should be fixed here.

[webpack-cli] Failed to load 'webpack.config.js' config
[webpack-cli] Error [ERR_REQUIRE_ESM]: require() of ES Module node_modules/@tanstack/router-plugin/node_modules/chalk/source/index.js from node_modules/@tanstack/router-plugin/dist/cjs/logger.cjs not supported.

Your Example Website or App

https://stackblitz.com/edit/github-q2lephbt-bqg6uxll?file=README.md

Steps to Reproduce the Bug or Issue

Node v18
Define a webpack.config.js

const {
  TanStackRouterWebpack
} = require('@tanstack/router-plugin/webpack')

run webpack cli with config

Expected behavior

It should not fail on chalk v5.

Screenshots or Videos

No response

Platform

Additional context

No response

Activity

  1. schiller-manuel commented on Jan 26, 2025

    @schiller-manuel
    Collaborator

    what would be a solution? replace chalk by a library that has both cjs and ESM?

  2. paypal-edveriksson commented on Jan 27, 2025

    @paypal-edveriksson
    Author

    I did a workaround by pinning chalk to v4 for now.

  3. velein commented on Jan 29, 2025

    @velein

    It is the same with Vite plugin.

    I had to pin both router and vite plugin like this to make it work (I guess I could play with even newer versions, but I was too lazy and these simply worked)

    "@tanstack/react-router": "1.82.0",
    "@tanstack/router-vite-plugin": "1.47.0",
    

    This is the error that shows up, if you are trying to use newer versions.

    An error occurred while processing files for the @nx/vite/plugin plugin.
        - xxx/vite.config.ts: require() of ES Module xxx/node_modules/@tanstack/router-plugin/node_modules/chalk/source/index.js from xxx/node_modules/@tanstack/router-plugin/dist/cjs/logger.cjs not supported.
      Instead change the require of index.js in xxx/node_modules/@tanstack/router-plugin/dist/cjs/logger.cjs to a dynamic import() which is available in all CommonJS modules.
          Error [ERR_REQUIRE_ESM]: require() of ES Module xxx/node_modules/@tanstack/router-plugin/node_modules/chalk/source/index.js from xxx/node_modules/@tanstack/router-plugin/dist/cjs/logger.cjs not supported.
          Instead change the require of index.js in xxx/node_modules/@tanstack/router-plugin/dist/cjs/logger.cjs to a dynamic import() which is available in all CommonJS modules.
    

    The simplest solution would be to downgrade chalk to 4.x 🤔

  4. added a commit that references this issue on Jan 30, 2025
    c0c7d77
  5. schiller-manuel commented on Jan 31, 2025

    @schiller-manuel
    Collaborator
  6. paypal-edveriksson commented on Jan 31, 2025

    @paypal-edveriksson
    Author

    Thank you, I can confirm that it fixed the issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions