Saturday, 24 February 2018

The 2018 Web Developer : Preparing a project for node_modules using npm

To prepare my project for node modules, I must initialise node within it's root directory.
First change to the project directory, using:
cd /myproject
Then type:
npm init -f -y
This will avoid me answering awkward questions that I'm not sure of, and provide me with a package.json file.
I'm also going to use the opportunity to make sure I'm got some tools in place such as the atom text editor, the slimjet browser and the LiveStyle plugins for atom and Chrome (which we can use in Slimjet).

atom

I won't go through the reasons for using atom, you can get that from many places, but needless to say I'm convinced. To install on Ubuntu:
sudo add-apt-repository ppa:webupd8team/atom
sudo apt update; sudo apt install atom

I had a graphic problem on an old laptop. It kept flashing. To remedy this I changed the command of my Atom launcher to:
/opt/atom/atom --disable-gpu
I installed the platformio-atom-ide-terminal package. This will open up a terminal inside the editor. It's quite useful because some commands, such as node-sass.
I also installed the livestyle-atom package.

Slimjet

Slimjet is a slim, fast browser which will give me all the benefits of Google Chrome browser by using its engine, but without telling Google everything I'm doing. To install:
wget http://www.slimjet.com/release/archive/8.0.4.0/slimjet_amd64.deb
sudo dpkg -i slimjet_amd64.deb
Now to add the livestyle extension. Within the browser:
More tools->Extensions->Get more extensions
Type livestyle

LiveStyle

LiveStyle is a tool for live CSS editing. It means I'll be able to edit my SASS file and see the changes in real-time within the Slimjet browser.

What next?

I now have some good tools in place for my project. I have a fast browser. I have an editor which allows me to make SASS changes and see them in real-time. I can do this by opening a terminal and starting the node-sass package. I can also use the terminal to compile changes to my .js files using node. I can also use the terminal to initialise my project to be node ready.
Now I can start adding local node_modules to my project in the next post.

The 2018 Web Developer : Adding node-sass globally

In the previous post I installed node.js (node). This also came bundled with the Node Package Manager (npm). npm allows me to install many packages created by the JavaScript community, both open-source and commercial. Examples include Twitter bootstrap, ReactJS, etc.
In this example I'm going install use node-sass, globally. This will allow us me develop CSS for any project using SASS on my system using.
From the command line type:
npm install -g node-sass
I then test that it has installed using:
node-sass -v

Using node-sass

In this first example, I have a terminal window open and I'm in a directory containing my custom.scss which will need compiling to custom.css in order to be used by my web pages.
node-sass -w custom.scss custom.css
In this second example, I'm in the root directory of my project with a subdirectory of /scss. Here, I am compiling all .scss files to their .css equivalents, in the same directory.
node-sass -w scss/ -o scss/

What next?

Now that I have used npm to install a package globally. I've applied a package, namely node-sass, which will improve my productivity. Now, in the next post, I need to begin a new project and prepare the ground for project specific packages.
I'm also going to use the opportunity to make sure I'm got some tools in place such as the Atom text editor, the slimjet browser and the livestyle plugins.

The 2018 Web Developer : Install node.js (node) on Ubuntu

In the last entry of this series, I installed Node Version Manager (nvm). This was important in order to give me the flexibility I need in this entry.

What is node.js (node)?

According to wikipedia "Node.js is an open-source, cross-platform JavaScript run-time environment for executing JavaScript code server-side".

How will node help me?

The first pleasure I got for using node.js was in testing JavaScript I'd written through the terminal. This meant, I could edit a file such jimmy.js, then test it on the command-line through:
node jimmy.js
No need to keep refreshing a browser. It also provided me with warning and error messages like any other compiler.
node also opens the door to much bigger things, particularly through the Node Package Manager, which I'll discuss in future posts. So it's well worth having.
Now to install node.js using nvm
nvm install node
Test that it has installed using:
node -v
If, for any reason, this call returns an error; node may not be in your PATH variable. To rectify this, open your .bashrc file, and at the bottom, add the line.
PATH=/usr/bin/node:$PATH

What next?

Now that I have nvm, npm and node installed what can I do?
Well I can begin by using node as a compiler for .js files. I mention this in the section titled "How will node help me?". I use the atom editor, which I'll be referring to in this series. So if I install the platformio-ide-terminal in atom, you can imagine how quick I'll be editing .js files now.
However, it's also good to know that I will face other, better improvements in my productivity with this new tool set. In the next post, I'll use npm to install node-sass globally. This will allow me to develop using SASS on all my projects.

Friday, 23 February 2018

The 2018 Web Developer : Install nvm on Ubuntu

In the introduction to this series I wrote about the value of learning JavaScript technologies such as node.js (node), in order to avail myself of more work. In order to install node I should first install nvm. This will allow me to control the versions of I install.

What is nvm?

Node Version Manager (nvm) is a tool for applying node to an installation.

How will nvm help me?

Let's say you're involved in a project which began and continues to be running using an older version of node. You, as a developer need to be in a position to install that older version in your project. You would obviously, also want to install newer versions for other projects.

OK, I'm convinced. Let's install stuff

Let's begin with nvm.
From your home directory, create a directory called .nvm
mkdir .nvm
sudo apt-get install build-essential libssl-dev curl git-core
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
Check the latest version from https://github.com/creationix/nvm
Now reboot and check that it works using:
nvm --version
A new directory should have been added to you home directory called .nvm
And the following should have been added to your .bashrc file
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

What next?

I haven't achieved much here, other than installing a piece of software, but I have opened the door to the possibilities which node brings. In the next instalment I'll take another baby step. I'll use my newly installed piece of software to install node.

The 2018 Web Developer : Introduction

I've been reading a lot of blogs lately by developers who have been in the same position as myself. We're people who have skills in HTML, CSS, jQuery, PHP, MySQL, Apache, GNU/Linux etc. These skills are still relevant. You can find still work with them, but increasingly, less work, in favour of node.js approaches to building applications.
node.js seemed to usher in a whole new set of methods in the unfortunate coincidence of developers having to adapt to using SASS, Git and everything else. Not to mention the fact that understanding node.js also came with a requirement to understand nvm and npm, etc. Not to mention the introduction of Grunt.js Gulp.js, Vue.js, etc.
As I started to familiarise myself with for example node.js, the setup seemed to require you to now install ruby, gem, yum or some other installer. Having got through all that, some blogger would tell me how great it was I could now spend the rest of my development life editing variables listed within package.json files. It didn't really look like fun.
In this next series of posts, I attempt to simplify the process of adopting some of these new technologies. I'm going to try and highlight why they are a useful addition to your existing skill set, rather than having to dispose of everything you already know. This will be done through the lens of an Ubuntu user of the Atom text editor.
If you want to skip through the first few baby steps I offer these commands to get you set up with node.js, nvm, npm and node-sass
sudo apt-get install build-essential libssl-dev curl git-core
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
Now restart
nvm install node
Now restart
npm install -g node-sass
Job done.

Wednesday, 10 January 2018

Editing command-line JavaScript, using Atom in Ubuntu

I've been doing a lot of JavaScript recently. It would have been too painful to render through the browser each time. So I set up an environment to do this within the Atom text editor. I use Ubuntu, although I'm sure the following instructions can be easily adapted to lesser operating systems.

Install nodejs
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
sudo apt-get install -y nodejs

Test this has worked by typing
node --version

Install Atom
sudo add-apt-repository ppa:webupd8team/atom
sudo apt update; sudo apt install atom


Add the terminal plugin to atom
Within atom type ctrl+, to get the settings window.
Click on install.
Type 'terminal'.
Now you will get a list of all terminal plugins for atom. I chose 'platformio-ide-terminal' and clicked the 'Install' button.
Restart atom.

Use the terminal within atom on your JavaScript
Open a directory containing your JavaScript. I suggest using the 'Add Project Folder...' option from the 'File' menu.
Let's imagine you have a directory such as /home/jimmy/js
If you haven't already, create a file such as 'hello.js'
Inside 'hello.js' add the line:
console.log('Hello world!');
Save the file.
Now at the bottom left-corner of Atom you will see a '+' symbol. Click the '+' and you will see a terminal window open in your chosen directory.
Click in the terminal window:
node hello.js
Hey presto!

Passing JavaScript arrays by reference and valuies

By default JavaScript arrays are passed by reference. See the code below. Notice that when a value in the 'firstArray' is changed, the corresponding value in 'secondArray' is also changed.

var firstArray = ['first','second','third'];
var secondArray = firstArray;

firstArray[1] = 'second most';

for (var i = 0; i < firstArray.length; i++)
{
  console.log(secondArray[i]);
}

/* Output
first
second most
third

*/

To overcome this issue you need to use the 'slice' method. This will pass the 'firstArray' to the 'secondArray' by value. See, in the output, that although a value in the 'firstArray' has changed, this hasn't affected the values within the 'secondArray'.

var firstArray = ['first','second','third'];
var secondArray = firstArray.slice();

firstArray[1] = 'second most';

for (var i = 0; i < firstArray.length; i++)
{
  console.log(secondArray[i]);
}

/* Output
first
second

third
*/