Setup Node.JS
Installing Node
On Ubuntu you can install Node.JS from the command line using apt-get
.
You should already have it installed on your laptop if you completed the codeX intro.
The installation instructions are as follows:
sudo apt-get install node-legacy
(this will instal Node asnode
and thenodejs
command)sudo apt-get install npm
Open a terminal window:
- type
node
and press enter - you should now be in a Node.JS console - you should get no errors. Use ctrl-C to get out of it - type
npm
you should see some information about npm on the console
Now you are ready to use JavaScript from the command line.
Node.JS JavaScript from the command line
Let’s create a JavaScript file and run it from the command line:
- Create a file called
hello.js
- Copy this text into the file:
console.log('hello world!');
- Save the file
- Now run it using
nodejs hello.js
- What happens?
So what happened? You liberated your JavaScript knowledge from the command line.
Now prove this to yourself and write some JavaScript that uses:
- an
if
statement - a
for
loop - a small function
- what else you need to get comfortable
If you can, create about 5 different JavaScript (.js) files to get more comfortable with JavaScript on the command line.
You can run the scripts using nodejs filename.js