python virtualenv with node environment via nodeenv

Virtualenvwrapper is one of the most useful tools a python user should be familiar with.  Built on top of virtualenv, it helps us avoid a lot of redundant “code administrative” work and simplifies our 3rd party python package isolation.

NodeJS has a similar isolation mechanism and a number of such isolation tools available.

I prefer to use the nodeenv tool because it saves me the trouble of typing out long “source” commands (much like virtualenvwrapper helps me avoid typing in long “source” commands provided by the virtualenv tool).  Also, the integration with virtualenvwrapper implies that I don’t have to waste time remembering new commands to manage my isolated nodejs environment.

So here’s how it’s done. Really efficiently! (assuming of course you have installed your python virtualenvwrapper and virtualenv tools properly):

$ mkvirtualenv myproject1
$ pip install nodeenv  # This installs the nodeenv package into our new python virtualenv so named "myproject1" above
$ nodeenv -p  # This commands installs nodejs and adds new shell functions to our virtualenvwrapper shell functions
* Install node.js (0.10.12) ..
* Appending nodeenv settings to /Users/calvin/.virtualenvs/myproject1/bin/activate
$ deactivate; workon project1   # Deactivate and re-activate to ensure we load in the updated shell functions and environment

Once this is done, we are all set with project1 being our nodejs AND python isolated environment.  When we use the `npm install -g` command, we install our npm packages into our node virtual env.

$ npm install -g yo grunt-cli bower

$ yo angular
[?] Would you like to include Twitter Bootstrap?: No
[?] Would you like to include angular-resource.js?: Yes
[?] Would you like to include angular-cookies.js?: Yes
[?] Would you like to include angular-sanitize.js?: Yes

This installs the `yo`, `grunt-cli` and `bower` tools directly into our node virtual environment and using `yo angular` we easily scaffold out a new angularjs project.

A quick peek at our ~/.virtualenvs/myproject1 directory shows us the directory structure that includes python packages and npm packages.


$ cd ~/.virtualenvs/myproject1

$ tree -L 2
.
├── bin
│   ├── activate
│   ├── activate.csh
│   ├── activate.fish
│   ├── activate_this.py
│   ├── bower -> ../lib/node_modules/bower/bin/bower
│   ├── easy_install
│   ├── easy_install-2.7
│   ├── get_env_details
│   ├── grunt -> ../lib/node_modules/grunt-cli/bin/grunt
│   ├── istanbul -> ../lib/node_modules/istanbul/lib/cli.js
│   ├── karma -> ../lib/node_modules/karma/bin/karma
│   ├── node
│   ├── nodeenv
│   ├── npm -> ../lib/node_modules/npm/bin/npm-cli.js
│   ├── pip
│   ├── pip-2.7
│   ├── postactivate
│   ├── postdeactivate
│   ├── preactivate
│   ├── predeactivate
│   ├── python
│   ├── python2 -> python
│   ├── python2.7 -> python
│   └── yo -> ../lib/node_modules/yo/bin/yo
├── include
│   └── python2.7 -> /opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7
├── install.cfg
├── lib
│   ├── dtrace
│   ├── node_modules
│   └── python2.7
├── share
│   └── man
├── src
│   └── node-v0.10.12
└── tags

That’s it.

node, npm and lessc on webfaction

I needed to get `lessc` on my webfaction server as I wanted to move some of my frontend css work on to less css.  Having used compass and grunt server via yeoman while messing around with a personal frontend-focused project, I am really digging how it was automating a lot of my css grunt work and making frontend development work a lot more enjoyable; so I wanted to find time to experiment with less compiler for use on this wordpress blog as well.

So here’s how I got it done.

Check that I have my `PATH` set correctly in `~/.bash_profile`.

export PATH=$HOME/bin:$PATH

Then, run the following in the terminal on my webfaction server.

1. Upgrade tar

mkdir -p $HOME/src
cd $HOME/src
wget http://ftp.gnu.org/gnu/tar/tar-1.26.tar.gz
tar -xvzf tar-1.26.tar.gz
cd tar-1.26
./configure --prefix=$HOME
make && make install

2. Install node

$ wget http://nodejs.org/dist/node-v0.6.9.tar.gz
$ tar -xvf node-v0.6.9.tar.gz
$ cd node-v0.6.9
$ ./configure --jobs=8 --prefix=$HOME
$ make && make install
$ which node
~/bin/node
$ node --version
v0.6.9

3. Install npm

$ curl https://npmjs.org/install.sh | sh
$ which npm
~/bin/npm
$ npm --version
1.1.71

4.  And finally, get less

$ npm install -g less
$ which lessc
~/bin/lessc
$ lessc --version
lessc 1.4.0 (LESS Compiler) [JavaScript]

That was straightforward! :-)