Développement

Create Node.JS development Environment on OSX

Preamble

Hi, a lot of people tell me that I make good articles but they are not written in French. So I decided to write it in English for this reason as well to improve my English skill

So I apologize in advance if I make horrible English mistakes.

Introduction

We’ll see how to install a Node.JS development environment on your mac. This environment contains :

  • Homebrew
  • Node.JS
  • MongoDB
  • Nginx
  • Redis
  • and an awesome Bonus
For this tutorial, I perform the installation on the most recent version achieved on Mac OSX : Yosemite. It is possible that some things are different depending on your version and the current state of your system.

Installation

Requirements

You must install the following software before continuing:

  • Xcode (to have gcc and other build requirements)
  • X11 server (for cairo library used for canvas npm module and others)

After installing the X11 server, you will need to add the following line in your ~/.bash_profile file :

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/opt/X11/lib/pkgconfig
Do not forget to run the source ~/.bash_profile command after changing your `~/.bash_profile` file.

Homebrew installation

Homebrew installs all the dependencies you need Apple does not provide you. It is used to install several libraries (gd, imagemagick, cairo, …) and servers (nginx, php, mysql, …).

To install Homebrew, it’s really simple. You just have to enter this line in your terminal :

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Homebrew will ask you to install Command-line tools. Accept and follow the instructions on the screen. At the end of the installation, enter the command brew doctor to see if Homebrew has been properly configured.

Installation of servers stack

Here is an important part of the article. In this part, we will install all servers for node.JS development :

  • Node.JS
  • Nginx
  • MongoDB
  • Redis (used for express and socket.io session storage)

To install the server, enter the following command in your terminal :

brew install nodejs mongodb nginx python redis

After this, run the following commands :

ln -sfv /usr/local/opt/nginx/*.plist ~/Library/LaunchAgents
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
ln -sfv /usr/local/opt/redis/*.plist ~/Library/LaunchAgents

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.redis.plist

These commands add the services to run servers in Mac OSX.

Finally, you can add the following line in your ~/.bash_profile file :

alias nodejs='node'    

I created this alias because, at work, I work on Linux, and the command to start NodeJS is nodejs. You can either start Node.JS with node or nodejs.

Install Node.js modules Dependencies

Several modules need to install libraries in Mac OSX so that they work properly. If you do not have the libraries required when installing a module, NPM will stop the installation. So I recommend you to install the following libraries to be quiet for most of the node modules :

brew install imagemagick gd icu4c cairo libpng libjpeg giflib

I recommend you then run the following command to avoid errors with modules requiring icu4c library.

brew link icu4c

Now all dependencies are installed.

Configuration

Nginx

Normally, Nginx works like Apache, with two directories to manage your virtual-hosts :

  • sites-available : Files for available websites but not necessary activated
  • sites-enabled : Files (essentially links to sites-available files) for enabled websites

But, in OSX, this directories are not created. To create this, run the following commands in your terminal :

cd /usr/local/etc/nginx
mkdir sites-available
mkdir sites-enabled

And edit your /usr/local/etc/nginx/nginx.conf file like this :

user  replacebyyourusername staff;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    include /usr/local/etc/nginx/sites-enabled/*;
}

If you look at the configuration file, you’ll notice the following line:

user  replacebyyourusername staff;

On Unix systems, it is necessary to run the Nginx server as root to use port 80. Under development sites are stored in our current directory, we need to tell Nginx with which user it runs. In our case, it must run as ourselves.

Redis

By default, the Redis master password is disabled. You must enable it by editing the /usr/local/etc/redis.conf file and looking for the following line:

#requirepass foo 

Uncomment this line and replace the default password that you choose.

MongoDB

For MongoDB, you must create an administrator for the server and a user for each database you will have. An article should be here soon to explain how to do it but in the meantime, I recommend you to see this mongodb official tutorial. I will update this article when it is written.

Bonus

Well, the article is completed, but before you leave, I give you some bonus to simplify server administration and software installation within OSX.

Homebrew Cask

Homebrew cask is an extension of Homebrew. It allows you to install, from the terminal, an application for Mac OSX without the dmg file. Enter this command in your terminal:

brew install caskroom/cask/brew-cask

We need Homebrew Cask for further bonuses.

Servers status monitoring

I know, on Mac OSX is not easy to start and stop a service. For example, this is the command to start mongoDB service :

launchctl start ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist

To simplify this, @jimbojsb developed launchrocket to manage the various services installed with Homebrew. To install, run the following command :

brew cask install launchrocket

Next, go to System Preferences. You should see the bottom of the window, the launchrocket button. Click it to access the Service Manager. If you do not see services, this is normal. Click on the button Scan Homebrew to refresh the homebrew installed services.

Note : You should click on this button after new service installation and after service upgrade.

Robomongo

To explore your mongodb databases and collections, I recommend you to use Robomongo. It’s simple and really helpful to test some aggregation requests, remove and add indexes, …

To install this with cask, execute this command :

brew cask install robomongo

Conclusion

Now, you have all the requirements to start your future NodeJS development. I know, with this article, i prefer show you how to install all new technologies with NoSQL and Node.JS.

I preferred to ignore MySQL because, nowadays, the major part of developers go on the couple NodeJS/MongoDB rather than NodeJS/MySQL.

I hope I have been around and not doing too many mistakes in this article. If you see or want to see new things in this article, do not hesitate to tell me in the comments.