Closed5

Flutter Firebaseを使おうとした際に、globalインストールしたnpmパッケージにPATHを通す方法が分からず試行錯誤した。

若槻龍太若槻龍太

検証

環境構築。

https://dev.classmethod.jp/articles/implementing-push-notifications-using-firebase-cloud-messaging-in-the-flutter-web-app

firebaseへのPATHが通っておらず、Flutterfireが実行できない。

$ flutterfire configure
i Found 0 Firebase projects.                                                                                                                                                         
FirebaseCommandException: An error occured on the Firebase CLI when attempting to run a command.
COMMAND: firebase --version 
ERROR: The FlutterFire CLI currently requires the official Firebase CLI to also be installed, see https://firebase.google.com/docs/cli#install_the_firebase_cli for how to install it.

npmの実行可能ファイルの場所はnpm binコマンドで確認可能。

https://docs.npmjs.com/cli/v7/commands/npm-bin

npmで実行可能ファイルがGlobalインストールされる場所は$HOME/.npm-global/binとのこと。

$ npm bin -g
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
/Users/wakatsuki.ryuta/.npm-global/bin

参考:
https://qiita.com/ebichan_88/items/e3e30461ad4ddd9368f5

$HOME/.npm-global/binのPATHを通したら実行可能になった。

$ export PATH=$PATH:$HOME/.npm-global/bin

$ firebase --version
11.22.0
若槻龍太若槻龍太

Globalインストールされたnpmパッケージは{prefix}/lib/node_modulesにインストールされ、実行可能ファイル(bin files)は{prefix}/binにリンクされる。

https://docs.npmjs.com/cli/v8/commands/npm-install#global

Operates in "global" mode, so that packages are installed into the prefix folder instead of the current working directory. See folders for more on the differences in behavior.

  • packages are installed into the {prefix}/lib/node_modules folder, instead of the current working directory.
  • bin files are linked to {prefix}/bin
若槻龍太若槻龍太

結論として次のコマンドを実行すればOK。

export PATH=$PATH:`npm prefix --location=global`/bin
若槻龍太若槻龍太

npm binコマンドだとPATHが通っていない場合に必ずwarningがプリントされるため見た目によろしくない。

$ export PATH=$PATH:`npm bin --location=global`
npm ERR! bin (not in PATH env variable)
このスクラップは2023/02/02にクローズされました