Javascript
Development Environment
Install Node: asdf-nodejs

Install node:how to install multiple versions with asdf-nodejs and check paths

In this post, we'll take a quick look at the JavaScript development environment and summarize the best way to install node, npm, and even yarn. In particular, we'll use a version manager to install node, which is a great way to prepare for the many versions of node you may use in the future.

1. Understanding the JavaScript Development Environment

JavaScript (JS for short) was created as a language for adding dynamic functionality to HTML in web browsers. As such, JS code could only be executed within a web browser.

But in 2009, Ryan Dahl released a JS runtime called node.js, based on the Chrome browser's V8 engine. It was now possible to run JS outside of a web browser.

nodejs logo
.

With node as a springboard, JS quickly became a mainstream language. In particular, its simple syntax, powerful asynchronous performance, event-loop structure, and vast community-supported libraries have made it a favorite of developers on all platforms, not just the Web. This JS development starts with installing node on any platform. Just like Java development starts with installing the JDK.

The easiest way to install node is to download the installer file from the official website and run it. However, this method requires you to go through a cumbersome setup process if you need multiple versions of node. To solve this problem, there are various version managers that can help you install and switch between different versions of JS.

The version manager we will use is asdf-nodejs.

2. asdf-nodejs

asdf-nodejs is a plugin that allows asdf to manage node.js. We can install various language and development tool plugins in asdf, allowing us to version control almost any language and tool with one program. See the asdf post for more information.

asdf-nodejs is one of four plugins that are officially maintained by the asdf team. So you can use it with confidence. The following steps assume you have installed and configured ASDF using the above post.

3. Installing the asdf-nodejs plugin

Everything that follows is the same for MacOS and Windows. Open the terminal you use on each OS and type the command below to add the plugin.

asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git

If you don't get any error messages, it was installed successfully. To verify that the plugin is working, type the following command, which will list all the node versions you can install.

asdf list all nodejs

Execution result: asdf list all nodejs results

As you can see, you'll get a lot of node versions. If you need a specific version, check to see if it's available for installation. If not, you can install the latest Long-Term Support (LTS) version for the service you're deploying, or the latest Current version.

Install lightly, as you can easily switch versions if you run into library compatibility issues.

Print only nodejs versions with certain conditions

As shown above, you can also use the asdf list all nodejs [specific_string] command to list only versions that contain a specific string.

4. Install the latest node with asdf-nodejs

Once you've decided which version of nodejs to install, let's install it. I'm going to install the LTS version. As the list all command shows, the latest node LTS versions have their own codenames. The latest LTS codenames can be found in the CHANGELOG.md file in the nodejs github (opens in a new tab) or in the CODENAMES.md file (opens in a new tab).

nodejs changelog on github

The command to install node is

asdf install nodejs [version_name]

For [version_name], you can enter any codename for the exact version hook. You can also specify lts or latest to install the latest LTS version or the current version, respectively.

Install the latest LTS version

After installation, run the following command to check which version you have installed on your machine.

asdf list nodejs

Execution result: Check the installed node version

5. Check the node js installation path

The different versions of node installed by asdf-nodejs are all stored inside ~/.asdf/installs/nodejs. You can also get the installation path of a specific version with the following command.

asdf where nodejs [version_name]

Execution result: Verify node installation path

Using the ls -al command, I can see that if I installed with a codename, it will install the correct version and create a link pointing to it.

6. Set to the installed node version

Now that you've downloaded and installed node, you need to configure your machine to use the node version you installed. asdf-nodejs allows you to set the desired node in three areas

  • Use in the current shell
  • Use in the current directory
  • Use everywhere in this computer

If all three scopes are set, they have the highest priority, in this order: shell, directory, and global.

6.1. Activate in the current shell

To activate only in the current shell, the command is

asdf shell nodejs [version_name]

In this case, if you close the shell by exiting the currently open terminal, the settings will be reset.

The result of the execution: asdf shell nodejs result

Check node version after shell re-run: Check node version after rerunning terminal

In the above example, you can see that the 19 version of node that was originally used on the machine was changed to 18 by the asdf shell command. After that, when the terminal was reopened and a new shell was opened, the settings were initialized.

6.2. Using in the current directory

Here's how to set the version of node used in the current directory. This can be very useful if you use different versions in different projects, as it will automatically load the version you set. The command is as follows

asdf local nodejs [version_name]

Let's check it out with an example.

Result of execution: node set to specific directory only

We've created a directory called node-test and used the command to enable the 18 version of node. The node used by the parent directory is the 19 version. However, if you enter the node-test directory, you will see that it automatically changes to the 18 version you set.

6.3. Enable machine wide

Finally, we can enable it on the whole machine. The command is as follows

asdf global nodejs [version_name]

Again, let's check with an example.

The result of the execution: node version changed

It was originally set to version 19, and when we typed the asdf list nodejs command, it was prefixed with *. I then changed it to the LTS version with the global command, and you can see that it has changed to that version. If you switch back to 19 with the same command, it will change normally.

7. Verify the path to the configured node.js executable file

For reference, the path to the node executable configured with asdf is always ~/.asdf/shims/node. ASDF runs the configured version of NODE using a technique commonly referred to as a shim. The path is shown below.

which node

Execution Result: Check path to node executable

8. Install npm

8.1. Check npm version

npm, the most popular package manager used by node, is installed with node when it is installed. Different node versions install different versions of npm, and the packages they install are managed differently. If you change your node version and then check the version with the npm --version command, you'll see that different node versions have different npm versions.

Result of execution: npm version check

8.2. Checking the npm installation path

The path of the installed npm executable is ~/.asdf/shims/npm, and the actual installed path is ~/.asdf/installs/nodejs/[version_name]/lib/node_modules/npm/bin/npm-cli.js.

Verify npm installation path

8.3.. Verifying the npx installation path

The path of the installed npm executable is ~/.asdf/shims/npx, and the actual installed path is ~/.asdf/installs/nodejs/[version_name]/lib/node_modules/npm/bin/npx-cli.js.

9. Install yarn

Along with npm, yarn, one of the most popular JS package managers, can also be easily installed using asdf. However, yarn always installs node first, so installing it with asdf will cause you to reinstall node. Therefore, we recommend installing it as a global package using npm for each node version.

With node installed globally, type the following command.

npm install --global yarn

Execution Result: Installing yarn globally using npm

If the installation completes successfully, check the package list with the following command.

npm list --global

The result of the execution: npm list --global results

9.1. Checking the yarn version

Finally, check the result of the yarn version check command.

yarn --version

Execution result: yarn version check

10. Conclusion

asdf-nodejs is one of the best ways to install node, npm and yarn. In particular, the ability to specify a node version in a specific folder and the ability to easily change the version you're using across your machine is something I think I'll be using a lot in the future. I hope this post helps you get started with JS development quickly and easily.

copyright for Javascript asdf-nodejs install

© 2023 All rights reserved.