When you install Node, it already comes bundled with a whole bunch of built in modules. 1
These are essentially libraries of code that the Node team wrote so that they can help you with day to 2
day things that you might want to do using Node. 3
Now, we mentioned that by liberating Javascript from the browser using Node, we can now use Node.js to interact 4
with the computer directly. 5
So a good example of that is we can use Node.js to get access directly to the local files of the computer. 6
So let's take a look at one of the built in modules for Node that helps us to do that. 7
So if you head over to nodejs.org/api, then you can see all of the native modules and the documentation 8
of how you can use it. 9
Now if we have a look in the left hand bar here, there's something called the file system. 10
And, as you might predict, this helps you interact with the local file system using Node. 11
Now there's a whole bunch of methods and properties that you can tap into, but in order to use the module 12
we first have to require it. 13
Now if we head over to Atom and add that new project folder that we created earlier on into our sidebar, 14
so intro-to-node, and we can delete our console.log in our index.js. Now instead of having that, 15
we're going to use the file system module, and we're going to write this line of code in order to use 16
this module. 17
So the first thing we write is a keyword called const, and up till now we've not been using this keyword 18
at all. 19
We've been using something called var. 20
Now var creates a variable where, say, for example, if I said var a = 2, then later on I decide to 21
change the value of a, a now equals 5, then this is how you can use vars to vary your variable. 22
Now const stands for constant, 23
and if I create a constant and I assign it a value, later on I can't change the value. 24
I can't then say a now equals 5. 25
It'll actually give me an error, and it'll tell me that attempting to override a, 26
which is a constant. 27
That means that once you've assigned it a value, you can't change constants any more. 28
And this is useful when you're creating containers that are not going to be changed or meddled with later 29
on. 30
So, for example, if in my web site I needed to use the value of pi, then pi is never going to change, right? 31
So I might just create a constant called pi and set it to 3.14159, and that's about all 32
I can remember. 33
So, in this case, we're creating a const called fs, and that stands for file system. 34
Now you can call this constant whatever you want. You can call it fileSystem. But for simplicity's sake we're 35
just going to call it fs. And we're going to set it to equal the file system module. And in order to use 36
libraries of code, or modules, or packages, inside our project, we have to require the module. And the module 37
is called fs. 38
So let's go ahead and add that in here. 39
So here's our brand new constant called fs, and it is set to equal the module that is fs. And we get access 40
to it 41
using this require method from Node. 42
Now, at the moment Atom is giving me a warning here, 43
as you can see from this little yellow dot, and it's telling me that the JSHint package, that I installed 44
to keep my Javascript code tidy and consistent with other programmers, is giving me a warning saying that 45
const is only available in ES6, it’s the latest stable version of Javascript at the time of recording. 46
And I can switch off these warnings by simply writing a comment that says jshint 47
esversion:6, 48
and this will tell the linter that I'm actually using ES Version 6, 49
don’t worry about the weird syntax, 50
this is simply a new feature of our 51
ES 52
Version 6 of Javascript. 53
So now that we've required fs, that means that we've incorporated this file system module into our project, 54
and we're now able to use it in our project. 55
Now, what are the sort of things that you can do using file system? 56
Well, there's actually a whole bunch of things that you can do, 57
for example, you can specify paths, you can open files, you can change files, you can access and read and 58
write to the local file system. 59
But, as we said before, we should be able to access and manipulate the local files if we're using Node.js. 60
So let's just use one of these methods to show you that this is possible. 61
So inside my intro-to-node, I'm going to create a new file, and I'm going to call it file1.txt. 62
So this is just a pure simple text file. 63
And inside here I'm just going to write a little bit of text, 64
“I am file 1.”, hit save, 65
and now I'm going to go into my index.js, and 66
I'm going to use this method called copyFileSync in order to create a copy of my file, 67
file1. 68
So our docs will tell us how to use it, and what we have to do is first, of course, require the module, 69
and then we're going to call the method and we're going to pass in two parameters. 70
So the first parameter is the source, 71
so that's the source filename to copy. 72
And the second one is the destination, which is the destination filename to copy to. 73
So that seems pretty easy. 74
So all you have to do is say fs.copyFileSync, and the source file is called file1.txt, and the destination 75
file, 76
we’ll just call it file2.txt. 77
So, what this line of code will do is look within the current directory, 78
so intro-to-node folder, it will look for a file that's called file1.txt, and then it will copy 79
it to something called file2.txt. 80
Now currently file2.txt doesn't exist. 81
So this is going to be the name of our copy. 82
So now if I hit save and I head over to Hyper, and make sure that you’re still within the directory that 83
our index.js is contained, which I've previously called intro-to-node. 84
Now here I'm going to use the node command, and then I'm going to run the index.js file using Node.js. 85
So now that I see my prompt again, so my name plus the dollar sign, 86
that means that this execution has completed. 87
So now, if we head into Atom and we look inside our folder, intro-to-node, you can see I now have two files, 88
file1, which still has the same content as before, and also file2, which now has the copied content 89
from file1. So if file2 already existed, then this copyFileSync will simply replace the existing content. 90
So, let's say that in file2 we have some text saying that “I am file 2.txt” and we hit save, and we 91
run our script again, 92
then it will again try to replace what's inside file2 with a copy of what's inside file1. 93
So file2 currently reads “I am file 2.txt”. 94
So, in order to run the last command, all you have to do is just hit the Up key on your keyboard, and 95
it will, 96
and you can cycle through all the previous commands that you've entered. 97
And this is just a quicker way of repeating commands. 98
So we're going to run that file again, 99
and now it's done. 100
And let's head over here. 101
So file2 now reads “I am file 1.” It’s cool, right? 102
So we're able to use Node.js to write Javascript code and execute it to affect our computer, including 103
manipulating and reading and writing to our local file system. 104
Now in the next module, we're going to talk about how we can use external modules, 105
so not just the modules that come bundled with Node, which are available here, 106
but actually there's a whole bunch of libraries that other people have written which we can tap into, 107
and we can use, using something called NPM. 108
So for all of that and more, I’ll see you on the next lesson.