Categories
MacOS X NPM

Installing & using Node & NPM on MacOS X

To install NPM & Node simply go to the Node web site and install the latest stable version from the downloads page; or use the link on the Node homepage.

To check the installation simply use the methods described below to check the respective versions of NPM & Node.

NPM

NPM stands for Node Package Manager and is a way to quick install packages for Node with all their dependencies.

To check your installed version of NPM from the terminal use:

npm -v

If you use NPM regularly you’ll get nudged occasionally to upgrade – if you see something similar to the following in your CLI, just follow the instructions:

A subtle nudge to update you NPM install – just follow the instructions.

If you need to update your version of NPM and you get errors with the above command, use the following to escalate your privileges:

sudo npm install -g npm stable

Afterwards, check your installed version:

All sorted, now running the latest stable version.

Node

Node is basically JavaScript running on the backend of your server – it can easily replace other more traditional backend languages such as PHP and Java meaning you can use JavaScript for your full stack.

To check your installed version of Node from the terminal use:

node -v

To upgrade Node, simply use NPM with the following command:

Removing NPM & Node from MacOS X

To remove both NPM & Node from your Mac, use the following commands:

cd /usr/local/lib

sudo rm -rf ./node*

cd /usr/local/include

sudo rm -rf ./node*

cd /usr/local/bin

sudo rm -rf ./npm

sudo rm -rf ./node

cd ~

rm -rf ./.npm

You can check to see if your NPM & Node installs return anything using the version command:

node -v

If there are any other areas of NPM or Node that require removal, let me know in the comments.

Categories
NPM

Node Package Manager – Installing packages

The most common task at the beginning of a project is to install the necessary packages – to do this you’ll use the install command a few times:

npm install react --save

To save a few keystrokes, this can be shorted to:

npm i react -S

npm i react-dom -S

--save or -S install the package to the current project – the packages will show up in the package.json under dependencies with a version number, usually the latest:

  "dependencies": {
    "react": "^16.12.0"
  }

Development dependencies

Not all dependencies need to be in the finished project, stuff like dev tools. To install a package only used during the development phase you can swap to --save-dev or -D:

npm i babel-core -D

This will install development dependencies under devDependencies in the package.json:

  "devDependencies": {
    "babel-core": "^6.26.3"
  }

Multiple packages in one install

If you are installing multiple packages at the same time, you can combine your installs into one line:

npm i babel-core babel-loader babel-preset-react -D

It doesn’t really matter if something has already been installed:

  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^8.0.6",
    "babel-preset-react": "^6.24.1"
  }

If you wanna get really hardcore with your installs, you can use the following syntax to combine common package names:

npm i babel-{core,loader} babel-preset-react -D

This hurts my head though.


Specific versions

If your project has very specific requirements for what packages are used, you can also load specific versions via NPM:

npm install lodash@4.1.8

Or the latest version in a major version:

npm install lodash@^4.0.0


What happens in the background?

Loading just 5 packages seems easy when you look at it like this, but if you check your projects node_modules folder you’ll see alot happened in the background:

Installing packages in NPM installs all the dependencies for every package.

Every package you install also brings with it all of its dependencies – this can add up quickly on a big project. Our 5 packages has installed 83 packages into our node_modules folder.

Not something you’d want to manage manually…