By Yeo Yong Kiat in ReactJS
NodeJS actually comes packaged with a library manager called NPM, which stands for Node Package Manager. NPM is used to install external node libraries via the commandline or terminal - if your app uses these libraries, these libraries are the so-called "dependencies" of your app. So in short, an extremely useful tool to have in your React toolkit as a beginner.
NPM should have been installed together with NodeJS, so run the following command in your terminal to check if it was successfully installed. You should see the version number of NPM as the output:npm --version
When working with JS libraries, there are two ways to install them. One way is to install them as global node packages, which means that they are accessible from anywhere within your terminal. Upon installation, you're set to go forever - you do this with a simple "-g" flag in your commandline:npm install -g <package>
More often, you'll probably be installing local node packages, where the library is accessible only by your application, in its local directory. To do so, you drop the "-g" flag:npm install <package>
For local node packages installed in the local directory of your app, the installed libraries will exist in a folder called node_modules and will be listed as explicit dependencies in your app's ".json" configuration files. We'll touch on those in a later post.
Another important command to learn is the "--save-dev" flag, which installs the node package only for use in your development environment. This comes in handy when testing a project, where you may only want to test out a library or framework, and don't want to use it for the actual production or deployment build. To do so, type the following command in your terminal when installing packages:npm install --save-dev <package>
Uninstalling packages can be done through the terminal as well, so there's no need for manual deletion of files through your document manager. You invoke the "uninstall" command:npm uninstall <package>
Alright, let's try to use NPM to install a local version of react for an app. Suppose we are coding a simple "calculator" app; so let's create a "calculator" folder to work in:mkdir calculator
And there you go! Well, this isn't quite the way to create a react app directory, but that isn't the point of this post anyway.
In the next post, we'll see how to create a react app directory the correct way.
Find more ReactJS stories on my blog. Have a suggestion? Contact me at [email protected].