事象
npmコマンドを実行時に Unexpected token '.'
が発生した。
$ npm update -g
npm ERR! Unexpected token '.'
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\***\AppData\Local\npm-cache\_logs\2023-09-20T15_29_23_397Z-debug-0.log
環境
npm 10.1.0
nvm 1.1.7
node.js 16.13.1
OS Windows10
原因
nvmとnodeのバージョンの相性が悪いから?と思われる。
npmのバージョンを確認すると警告が出力されるけど関係あるのかな??
$ npm -v
npm WARN cli npm v10.1.0 does not support Node.js v16.13.1. This version of npm supports the following node versions: `^18.17.0 || >=20.5.0`. You can find the latest version at https://nodejs.org/.
10.1.0
対応方法
出力されたログを参考に使用するnodeのバージョンを切り替えれば解消する。
This version of npm supports the following node versions: `^18.17.0 || >=20.5.0`.
今回だと 18.17.0
がよいみたい。
$ nvm install 18.17.0
Downloading node.js version 18.17.0 (64-bit)...
Extracting node and npm...
バージョン切り替え後、失敗したnpmコマンドを再実行すると正常終了する。
$ npm update -g
removed 12 packages, and changed 78 packages in 12s
$ nvm use 18.17.0
Now using node v18.17.0 (64-bit)