Use yarn to install and manage a package globally, available across all the projects on your machine
Working with Yarn Global Commands
An alternative to NPM --global option

With the advent of NPM (Node Package Manager), each project maintains dependencies in its own node_modules. However, we install a few packages, tools, and utilities at the global level. These packages are available across projects and directories. In an example, it is appropriate to install the following tools and utilities at the global level — Angular CLI, create-react-app, http-server, json-server, etc.
The npm install command with --global option copies packages to a global directory. However, Yarn is “arguably” better than NPM for its speed and security. This article details using Yarn to install tools or packages at the global level.
With Yarn, all global commands need to start with yarn global. Consider the following snippet.
# The following command lists information about all global packages
yarn global list# The following command uninstalls Json Server package
# at global level
yarn global remove json-server
To add a package at the global level, use the following command. It installs Http Server, available to use across projects and directories.
yarn global add http-server
Http Server is a Node.js based zero configuration http server highly useful for rendering static content, application bundles and more.
Gotcha
After successful installation, run http-server in any directory or project. It should start the Node server. In few cases, it might not work. Let’s explore this further!
Where did it install the executable and package for Http Server? Consider the following commands. When you run the http-server command, the executable in the bin runs to start the http server.
# 1. Print yarn directory with symlinks to executable.
yarn global bin# 2. Print yarn directory for global node_modules
yarn global dir
It’s possible, the terminal (or command prompt) doesn’t know the yarn bin directory location. Set path to this directory so that the terminal looks for the executable here.
# On Windows
set PATH=%PATH%;C:\Users\my_user_name\AppData\Local\Yarn\bin# On macOS
export PATH="$(yarn global bin):$PATH"
Run http-server to start the Node.js http server from any directory or project.
Add to a custom directory
You may add the package to a custom directory using --prefix option. consider the following snippet
yarn global add http-server --prefix C:\yarn-temp
Now, the symlinks to Http Server executable are copied to C:\yarn-temp\bin instead of the default (or the configured location).
c:\yarn-temp\bin>dir
Volume in drive C is Windows
Volume Serial Number is 24C0-1F85Directory of c:\yarn-temp\bin26-05-2021 14:19 <DIR> .
26-05-2021 14:19 <DIR> ..
26-05-2021 14:19 414 hs
26-05-2021 14:19 90 hs.cmd
26-05-2021 14:19 432 http-server
26-05-2021 14:19 99 http-server.cmd
4 File(s) 1,035 bytes
2 Dir(s) 434,982,051,840 bytes free
Happy Coding! By the way, use the following links to know more about me and my opinions
References and useful links
- Link to yarn global documentation https://classic.yarnpkg.com/en/docs/cli/global/
- Link to Http Server GitHub read-me https://www.npmjs.com/package/http-server
More content at plainenglish.io