ホームページ  >  記事  >  ウェブフロントエンド  >  NVM をインストールして使用して複数の Node.js バージョンを管理する方法

NVM をインストールして使用して複数の Node.js バージョンを管理する方法

Patricia Arquette
Patricia Arquetteオリジナル
2024-10-12 18:43:02438ブラウズ

How to Install and Use NVM to Manage Multiple Node.js Versions

導入

ハイテク愛好家の皆様、こんにちは!

Windows 用ノード バージョン マネージャー (NVM) の使用に関するステップバイステップ ガイドへようこそ。異なるプロジェクト間で複数のバージョンの Node.js を管理するという課題に直面したことがある場合、NVM は必要なツールです。これによりプロセスが簡素化され、Node.js のバージョンを簡単に切り替えることができ、開発環境を整理した状態に保つことができます。

この記事を終えるまでに、Windows に NVM をインストールし、NVM を使用して Node.js のバージョンを管理し、Node.js ワークフローをよりスムーズにできるようになります。

記事の目的

この記事では以下について説明します:

  • NVM とは何か、Node.js 開発にとって NVM が重要である理由。
  • Windows に NVM をインストールする方法。
  • Node.js のバージョンを管理するための必須の NVM コマンド。
  • NVM を効果的に使用するための一般的な使用例とヒント。

さあ、飛び込みましょう!

NVM とは何ですか?なぜ使用するのですか?

Node Version Manager (NVM) は、システム上の Node.js の複数のバージョンを管理するのに役立つツールです。開発者は、異なる Node.js バージョンを必要とするプロジェクトに取り組んでいるかもしれません。これらのバージョン間を手動で切り替えると、時間がかかり、エラーが発生しやすくなります。

NVM を使用すると、次のことが可能になります。

  • 複数の Node.js バージョンを簡単にインストールして使用します。
  • 単一のコマンドでバージョンを数秒で切り替えます。
  • .nvmrc ファイルを使用して、プロジェクト固有のバージョンの Node.js を設定します。

NVM を使用すると、プロジェクトで別のバージョンが必要になるたびに Node.js をアンインストールして再インストールする必要がなくなります。


Windows に NVM をインストールする方法

Windows への NVM のインストールは簡単ですが、macOS や Linux でのプロセスとは少し異なります。ステップバイステップのガイドは次のとおりです:

  1. Windows 用 NVM をダウンロード

    • Windows 用 NVM の公式 GitHub リリース ページにアクセスしてください。
    • 最新の NVM-Setup.zip ファイルをダウンロードします。
    • ZIP を解凍し、nvm-setup.exe ファイルを実行してインストールを開始します。
  2. インストールを完了します

    • インストーラーのプロンプトに従います。変更する特別な理由がない限り、デフォルトのインストール場所 (C:Program Filesnodejs など) を使用することをお勧めします。
  3. 環境を構成する

    • インストール後、コマンド プロンプト または PowerShell を開き、次のコマンドを実行して NVM が動作していることを確認します。
     nvm version
    
  • NVM バージョンが出力されていれば、インストールは成功しています。

Windows 用の基本的な NVM コマンド

NVM がインストールされたので、マシン上の Node.js バージョンを管理するために必要な最も便利なコマンドを見てみましょう。

  • 特定の Node.js バージョンをインストールする
   nvm install 14.17.0

このコマンドは、Node.js バージョン 14.17.0 をシステムにダウンロードしてインストールします。 14.17.0 は、必要なバージョン番号に置き換えることができます。

  • 別の Node.js バージョンに切り替える
   nvm use 14.17.0

これにより、アクティブな Node.js バージョンを 14.17.0 に切り替えるように NVM に指示されます。

  • デフォルトの Node.js バージョンを設定する 特定のバージョンを常にデフォルトで使用したい場合は、次を実行します。
   nvm alias default 14.17.0
  • インストールされている Node.js バージョンをリストする
   nvm list

このコマンドは、NVM を使用してインストールしたすべての Node.js バージョンを一覧表示します。

  • 利用可能なすべての Node.js バージョンをリストする インストールできる Node.js のすべてのバージョンを確認するには、次を使用します。
   nvm list available
  1. Node.js バージョンをアンインストールします 特定のバージョンの Node.js が不要になった場合は、次のコマンドを使用して削除します。
   nvm uninstall 14.17.0

プロジェクト内の Node.js バージョンの管理

NVM の主な利点の 1 つは、個々のプロジェクトに Node.js のバージョンを指定できることです。

.nvmrc を使用して Node.js バージョンを指定する

プロジェクトで常に特定の Node.js バージョンが使用されるようにするには、プロジェクトのルート ディレクトリに .nvmrc ファイルを作成します。

たとえば、プロジェクトに Node.js バージョン 14.17.0 が必要な場合は、次の内容を含む .nvmrc ファイルを作成します。

14.17.0

次に、そのプロジェクト ディレクトリ内にいるときに、次のコマンドを実行します。

nvm use

NVM は、.nvmrc ファイルで指定されたバージョンに自動的に切り替わります。


Common Use Cases

  1. Switching Node.js Versions for Different Projects
    If you’re working on multiple projects, each requiring a different Node.js version, use nvm use to switch between them seamlessly.

  2. Testing Your Application Across Different Node.js Versions
    NVM makes it easy to test your application’s compatibility with various Node.js versions. Simply switch versions using nvm use and run your tests.

  3. Isolating Global Packages
    Global npm packages are installed separately for each Node.js version. This means you can have different global packages for each version without worrying about conflicts.


Pro Tips for Using NVM on Windows

  • Speed Up Switching Between Versions You can use the nvm alias command to create shortcuts for switching to commonly used versions. For example:
  nvm alias lts 14.17.0

Now, you can switch to Node.js version 14.17.0 by simply running nvm use lts.

  • Install Node.js LTS Versions You can install the latest long-term support (LTS) version of Node.js by running:
  nvm install --lts
  • Handling Global npm Packages Keep in mind that global npm packages are installed per Node.js version. If you switch versions and notice a global package missing, you’ll need to reinstall it for that specific version.

Conclusion

Node Version Manager (NVM) is an essential tool for any Node.js developer, especially if you’re working with multiple projects or need to test code across different Node.js versions. With NVM, you can install, switch, and manage Node.js versions effortlessly, making your development process much smoother.

Now that you have NVM set up and running on Windows, you’re ready to manage Node.js versions like a pro. Start switching versions with ease, and ensure each of your projects is running in the right environment.


Stay Connected

If you have any questions or need further assistance with NVM, feel free to reach out! I’d love to hear your feedback or help you on your development journey.

  • Email : [gunasantosh4@gmail.com]
  • LinkedIn : Guna-Santosh
  • GitHub : Guna-Santosh

Thank you for reading, and happy coding with NVM!

以上がNVM をインストールして使用して複数の Node.js バージョンを管理する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。