mermaid-js / mermaid-cli

Command line tool for the Mermaid library
MIT License
2.31k stars 223 forks source link

syntaxerror: Unexpected token '.' (Node.JS version is too old) #587

Closed Damianus500 closed 12 months ago

Damianus500 commented 1 year ago

Did an FRESH installation of mermaid-cli on my popos22 laptop.

sudo npm install -g @mermaid-js/mermaid-cl

No error.

My system is:

OS: Pop!_OS 22.04 LTS x86_64 Host: HP Laptop 15s-eq0xxx Kernel: 6.4.6-76060406-generic Uptime: 1 hour, 38 mins Packages: 3244 (dpkg), 66 (flatpak) Shell: bash 5.1.16 Resolution: 1920x1080 DE: GNOME 42.5 WM: Mutter WM Theme: Pop Theme: Materia-dark-compact [GTK2/3] Icons: gnome [GTK2/3] Terminal: tmux CPU: AMD Ryzen 7 3700U with Radeon Vega Mobile Gfx (8) @ 2.300GHz GPU: AMD ATI Radeon Vega Series / Radeon Vega Mobile Series Memory: 6270MiB / 13881MiB

The error I received is:

⚝ Wed Aug 23 10:53 靜 ᗫ ⚝ ↬ mmdc file:///usr/local/lib/node_modules/@mermaid-js/mermaid-cli/src/index.js:262 const svg = container.getElementsByTagName?.('svg')?.[0] ^

SyntaxError: Unexpected token '.' at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18)


I have little experience with npm. Pls help me to troubleshoot.

Thank you!

DL

LingkKang commented 1 year ago

Too long, don't read: possibly check & update your node / npm version


Just met the same question a few minutes ago.

This is probably because node or npm version is too low. Because I noticed that when I was installing mermaid-cli with sudo npm install -g @mermaid-js/mermaid-cli, there are some warnings stated npm WARN EBADENGINE (see below).

npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: '@mermaid-js/mermaid-cli@10.3.1',
npm WARN EBADENGINE   required: { node: '^14.13 || >=16.0' },
npm WARN EBADENGINE   current: { node: 'v12.22.12', npm: '7.5.2' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'commander@10.0.1',
npm WARN EBADENGINE   required: { node: '>=14' },
npm WARN EBADENGINE   current: { node: 'v12.22.12', npm: '7.5.2' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: '@puppeteer/browsers@0.5.0',
npm WARN EBADENGINE   required: { node: '>=14.1.0' },
npm WARN EBADENGINE   current: { node: 'v12.22.12', npm: '7.5.2' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'cosmiconfig@8.1.3',
npm WARN EBADENGINE   required: { node: '>=14' },
npm WARN EBADENGINE   current: { node: 'v12.22.12', npm: '7.5.2' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'puppeteer-core@19.11.1',
npm WARN EBADENGINE   required: { node: '>=14.14.0' },
npm WARN EBADENGINE   current: { node: 'v12.22.12', npm: '7.5.2' }
npm WARN EBADENGINE }

Even though the installation succeed, running any command such as mmdc --version or mmdc --help would lead to error:

~$ mmdc --version
file:///usr/local/lib/node_modules/@mermaid-js/mermaid-cli/src/index.js:262
      const svg = container.getElementsByTagName?.('svg')?.[0]
                                                 ^

SyntaxError: Unexpected token '.'
    at Loader.moduleStrategy (internal/modules/esm/translators.js:133:18)

At this time, I have nodejs version 12.22.12 and npm version 7.5.2. This is because the source of apt do not have any newer version of them.

Following the node official page Installing Node.js via package manager | Node.js, for my platform, I used this method. Don't forget to re-install mermaid-cli. And now, everything is fine:

~$ node -v
v18.17.1
~$ npm -v
9.6.7
~$ mmdc --version
10.3.1

This also satisfied the version requirement (I'm not a pro on js project, but I think this is likely to be) of mermaid-cli repo:

https://github.com/mermaid-js/mermaid-cli/blob/595ce1bcaa29fa5c890a5c6d20a9a00e6e856bb0/package.json#L12-L14

For your information, I'm using a simple application server from Alibaba Cloud, with Debian 11 installed. But this shouldn't have any influence on the error I suppose.

Hope this can help :)

aloisklink commented 12 months ago

@LingkKang is exactly right!

Unfortunately, Node.JS updates very quickly, and even the LTS versions become deprecated after 2.5 years, see https://github.com/nodejs/release#release-schedule.

This means that we have to very quickly drop support for old Node.JS versions, because of all our dependencies drop support for old Node.JS versions. But Debian/Ubuntu usually only comes with very old versions of Node.JS, that may have security vulnerabilities.

Following the node official page Installing Node.js via package manager | Node.js, for my platform, I used this method.

That's a good way to do things!

Duplicate of https://github.com/mermaid-js/mermaid-cli/issues/572.