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

i have problem to import mermaid from "mermaid" #5081

Closed
FaryarKankash opened this issue Nov 28, 2023 · 5 comments
Closed

i have problem to import mermaid from "mermaid" #5081

FaryarKankash opened this issue Nov 28, 2023 · 5 comments
Labels
Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect

Comments

@FaryarKankash
Copy link

Description

i cant use mermaid in my node js project, every time i want to import it , it give me an error
the error is:
Error [ERR_REQUIRE_ESM]: require() of ES Module C:**\Desktop\mermaid-js\node_modules\mermaid\dist\mermaid.core.mjs not supported.
Instead change the require of C:\Users\farya\Desktop\mermaid-js\node_modules\mermaid\dist\mermaid.core.mjs to a dynamic import() which is available in all CommonJS modules.

node version: 18.16.1
npm version: 9.5.1

Steps to reproduce

  1. simple just import mermaid from "mermaid"
  2. use it console.log(mermaid);
  3. ts-node index.ts

Screenshots

No response

Code Sample

import mermaid from "mermaid"

console.log(mermaid);

Setup

  • Mermaid version:
  • Browser and Version: [Chrome, Edge, Firefox]

Suggested Solutions

No response

Additional Context

No response

@FaryarKankash FaryarKankash added Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect labels Nov 28, 2023
@sidharthv96
Copy link
Member

You're getting this error because mermaid is an ESM package, and you're trying to import into a CommonJS project.
Please refer https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c on ways to use ESM inside CJS.

You can also refer these links for more info.

@vvscode
Copy link

vvscode commented Dec 11, 2023

@sidharthv96 sorry, but I get the same error on using cli

➜  __tools git:(master) ✗ npx mmdc _logs/mermaid.mmd
/Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js:2
const stripAnsi = require('strip-ansi');
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/strip-ansi/index.js from /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js not supported.
Instead change the require of /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/strip-ansi/index.js in /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js:2:19) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.4.0

issues happens on installing mermaid-cli as dev dependency and running with npx, but it doesn't appear on global install. I would expect normal work in local setup as well. Is my expectation wrong?

@sidharthv96
Copy link
Member

@vvscode I'm not sure how close your error is related to mermaid. As the package is not mentioned in the stack trace. The CLI also doesn't require mermaid directly, but imports it inside a webpage. @aloisklink can confirm.

@vvscode
Copy link

vvscode commented Dec 12, 2023

➜  __tools git:(master) ✗ node node_modules/@mermaid-js/mermaid-cli/src/cli.js
/Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js:2
const stripAnsi = require('strip-ansi');
                  ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/strip-ansi/index.js from /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js not supported.
Instead change the require of /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/strip-ansi/index.js in /Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/string-width/index.js:2:19)
    at Object.<anonymous> (/Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/cliui/build/index.cjs:291:21)
    at Object.<anonymous> (/Users/v.vanchuk/repo/pwa-ib/__tools/node_modules/yargs/build/index.cjs:1:60683)
    at async Promise.all (index 0) {
  code: 'ERR_REQUIRE_ESM'
}

does this make more clear, that the problem is somewhere there? =)

UPD: tried to setup a demo repo and discovered that I'm not able to reproduce it in a fresh repo. Even when I take the same package.json file. This makes me think that my report is invalid. I'm confused.

Sorry for noise

UPD2: after removing yarn.lock the problem gone in my repo as well

@aloisklink
Copy link
Member

Hmmmmm, @vvscode, it looked like you somehow downloaded a version of string-width that was incompatible with your version of strip-ansi. Maybe an older version of these packages had a bug in them?

It's good that you got the issue fixed, but if you do have any other issues with the CLI, please feel free to make an issue in https://github.com/mermaid-js/mermaid-cli :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Triage Needs to be verified, categorized, etc Type: Bug / Error Something isn't working or is incorrect
Projects
None yet
Development

No branches or pull requests

4 participants