From the course: Learning Functional Programming with JavaScript ES6+

Setting up the project - JavaScript Tutorial

From the course: Learning Functional Programming with JavaScript ES6+

Start my 1-month free trial

Setting up the project

- Before we get into learning functional programming with JavaScript ES6, let's set up an NPM directory that we can use to run our ES6 code. Open up a terminal and use cd to navigate to whatever parent directory you want to store the directory for this course in. I'm going to use Documents. And then us the make directory command to create a new directory. For example, functional-ES6. And then use cd to move into that directory. And once we're inside here, we want to set up our directory as an NPM package. And we'll do that by running NPM init dash y, and the y flag here sets up an NPM package with all the default values. If you want to override the default values for your directory, just leave off the dash y flag. And now that we've set up our directory with NPM, we can install the NPM packages necessary to be able to execute ES6 code with node JS. Currently, node JS doesn't have native support for ES6 syntax, so we need to use a tool called babel to act as the middleman between the modern ES6 syntax that we're going to write, and common JS syntax which node JS can run. This might sound complicated, but it isn't. There are just two steps we need to take in order to get it working. The first thing we need to do is install the necessary bable packages. And we do this by simply running NPM install, dash dash save, at babel slash core, at babel slash node, and at babel slash preset, dash env. And hit enter. And this will run for a little while, but once it finishes, the second thing we need to do is create a new file in our directory. In order to do that, let's open up our directory in an IDE. I'm going to use VSCode here, but feel free to use whatever editor you're comfortable with. So, let's open our directory, mine is in Documents, functional ES6. And inside this directory, we're going to create a new file, and we're going to call it dot babel rc. I'm going to zoom in a little bit here, so that you can see it better. And all we need to do here is create a json object, with a single property called presets. And the value of this property will just be an array with a single string, that says at babel slash preset, dash env. And then we save that file, and that's all we have to do. We shouldn't have to worry about this file ever again for the rest of the course. Now comes the most important part, running our code. From now on, I'm going to be running our code from VSCode's built-in terminal, but you're free to use whatever terminal you want. So, normally in node JS, we can run our code by typing node, and then the path to our file, and the file name. So for example, source slash my file dot js. But since we're going to be writing our code in ES6 syntax, which isn't yet natively supported by node, the command we're going to use instead is npx babel node, and then the path to our file.

Contents