question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Chakra CLI does not work with ESM packages

See original GitHub issue

Description

When running @chakra-ui/cli tokens, it doesn’t work with ESM packages

Link to Reproduction

https://github.com/kwajiehao/chakra-ui-cli-esm-bug

Steps to reproduce

  1. Go to the sample repo
  2. Run npm install
  3. See error:
Error: Cannot find module 'src/theme/index.ts'
Require stack:
- <path>/.npm/_npx/de2e382d70c335ff/node_modules/@chakra-ui/cli/dist/scripts/read-theme-file.worker.js
Error: Theme file or package not found 
Error: Must use import to load ES Module: <path>/chakra-ui-cli-esm-bug/src/theme/index.ts
require() of ES modules is not supported.
require() of  <path>/chakra-ui-cli-esm-bug/src/theme/index.ts from  <path>/.npm/_npx/de2e382d70c335ff/node_modules/@chakra-ui/cli/dist/scripts/read-theme-file.worker.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from  <path>/chakra-ui-cli-esm-bug/package.json.

Error: Cannot find module 'src/theme/index.ts'
Require stack:
- <path>/.npm/_npx/de2e382d70c335ff/node_modules/@chakra-ui/cli/dist/scripts/read-theme-file.worker.js
    at readTheme (<path>/.npm/_npx/de2e382d70c335ff/node_modules/@chakra-ui/cli/dist/scripts/read-theme-file.worker.js:86:13)
    at async run (<path>/.npm/_npx/de2e382d70c335ff/node_modules/@chakra-ui/cli/dist/scripts/read-theme-file.worker.js:103:17)

Running an older version of Chakra CLI did not fix this issue

Chakra UI Version

@chakra-ui/cli v2.1.7

Browser

No response

Operating System

  • macOS
  • Windows
  • Linux

Additional Information

N/A

Tagging my colleague @karrui for follow ups

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6

github_iconTop GitHub Comments

2reactions
BeanWeicommented, Nov 3, 2022

Here is an example involving Vite (and thus Astro): https://stackblitz.com/edit/vitejs-vite-xfwglb?file=vite.config.ts

After running npm run build, starting the server with node dist/main.js will fail.

The reason is Chakra UI doesn’t work with ESM yet. The following code is an example:

// package.json
{
  "type": "module",
  "dependencies": {
    "@chakra-ui/react": "2.3.5"
  }
}
// index.js
import { Button } from '@chakra-ui/react';
// output
file:///home/projects/vitejs-vite-xfwglb/module-test/index.js:1
import { Button } from '@chakra-ui/react';
         ^^^^^^
SyntaxError: Named export 'Button' not found. The requested module '@chakra-ui/react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@chakra-ui/react';
const { Button } = pkg;

    at _instantiate (https://vitejs-vite-xfwglb.w-corp.staticblitz.com/blitz.c3f3eab0760382367e86f2a862c18516376c36f4.js:6:986717)
    at <anonymous> (<anonymous>)

The root cause is mentioned in this comment: #6783 (comment). By renaming all esm.js to esm.mjs, I managed to get the code to run.

Same issue with me. How can we fix it?

1reaction
serverwentdowncommented, Oct 7, 2022

Here is an example involving Vite (and thus Astro): https://stackblitz.com/edit/vitejs-vite-xfwglb?file=vite.config.ts

After running npm run build, starting the server with node dist/main.js will fail.

The reason is Chakra UI doesn’t work with ESM yet. The following code is an example:

// package.json
{
  "type": "module",
  "dependencies": {
    "@chakra-ui/react": "2.3.5"
  }
}
// index.js
import { Button } from '@chakra-ui/react';
// output
file:///home/projects/vitejs-vite-xfwglb/module-test/index.js:1
import { Button } from '@chakra-ui/react';
         ^^^^^^
SyntaxError: Named export 'Button' not found. The requested module '@chakra-ui/react' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@chakra-ui/react';
const { Button } = pkg;

    at _instantiate (https://vitejs-vite-xfwglb.w-corp.staticblitz.com/blitz.c3f3eab0760382367e86f2a862c18516376c36f4.js:6:986717)
    at <anonymous> (<anonymous>)

The root cause is mentioned in this comment: https://github.com/chakra-ui/chakra-ui/issues/6783#issuecomment-1267496257. By renaming all esm.js to esm.mjs, I managed to get the code to run.

Read more comments on GitHub >

github_iconTop Results From Across the Web

npm chakra-ui install failing due to dependency conflicts
It seems like it created a folder running some tests. Then I ran: npm i -g @vue/cli-service-global. . Then: npm run serve-- to...
Read more >
@chakra-ui/react-use-previous - NPM Package Scripts - Socket
React hook to track previous value. Version: 2.0.2 was published by segunadebayo. Start using Socket to analyze @chakra-ui/react-use-previous and its 0 ...
Read more >
Installation - Chakra UI
How to install and set up Chakra UI in your project. ... Version 2 of Chakra UI is only compatible with React 18....
Read more >
Gatsby Changelog | 5.3.0
The GraphQL schema: Changes to sort and aggregation fields change enables lower resource usage (significantly reduced memory usage) and faster “Building schema” ...
Read more >
Build a component library with React and TypeScript
Get notified of impactful user issues, not false positives. Less alerts, way more useful signal. The LogRocket Redux middleware package adds an ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found