Packages
Node Js works off of the concept that you can create and share Js files, that can be grouped into "packages". Theses "packages" work similar to Python "modules" in that you import the "modules" you wish to use. More information about Node modules and packages can be
found here.
You're not limited to packages that others have created, you can create them on your own. In order to create a package you need to be able to import and export files and folders. We can demonstrate the concept by creating a package that we can "share" with another Js file. We're going to create a Js file called [math.js]. Inside this file, all we are going to have is two functions, and one variable assignment.
Here is the file contents:
- JavaScript
-
- const add = ( x, y ) => x + y;
- const square = x => x * x;
- const PI = 3.14159;
So we've created a Js package that we can import into our other Js files. For that, we're going to need another Js file to work with. We'll call it [app.js]. Now in our new [app.js], we need to "import" our [math.js], and to do that, we use require like so:
- JavaScript
-
- const mathImp = require ('./math');
- console.log(mathImp);
Now if we run this in our "node" console, what will we get?
Well, we actually get:
an empty object. Now npm packages are in fact objects, so that part is correct, but we didn't get anything else from the package. This is because in the original [math.js] that we are trying to import, you have to specifically designate, what you want to export (share).
We do that with module.exports, like so:
- JavaScript
-
- const add = ( x, y ) => x + y;
- const square = x => x * x;
- const PI = 3.14159;
-
module.exports.add = add;
module.exports.square = square;
And now, if we run our [app.js], we now get:
- { add: [ Function: add ], square: [ Function: square ], PI: 3.14159 }
our complete package object with two functions, and one variable.
So now, from within [app.js], we can log out some results from our [math.js], by adding:
- JavaScript
-
- const {add, square, PI} = require ('./math')
- console.log(add( 2, 2 ));
- console.log(square(5));
- console.log(PI);
and we get:
Simplifying the Code
We can simplify our "exports" code in our [math.js] file by creating an object and exporting the object file like so:
- const math = {
- add: add,
- square: square,
- PI: PI
- }
- module.exports = math;
Another way to simplify the code would be to export each "math" item directly, like so:
- module.exports.add = ( x, y ) => x + y;
- module.exports.square = x => x * x;
- module.exports.PI = 3.14159;
In Summary
So we can "import packages" into another Js file by using the require keyword, in the file that we are importing into.
- const variable = require ('./file to import');
We can only import items that have been exported in the originating package with the module.exports keyword, eg:
- module.exports.add = ( x, y ) => x + y;
Final Code
- JavaScript
-
- module.exports.add = ( x, y ) => x + y;
- module.exports.square = x => x * x;
- module.exports.PI = 3.14159;
- JavaScript
-
- const {add, square, PI} = require ('./math');
- console.log(add(2, 2));
- console.log(square(5));
- console.log(PI);
And if you run app.js, it outputs:
One Last Note
Now, one other note about imports. We can also import an entire folder of files. To do this you need to create your packages in a folder, and the create a file that must be named index.html. Lets demonstrate. We'll create a folder called "pets, and inside the folder, we will create three files with different pet information. So in our file structure, we have:
- pets
-
- chico.js
- jitters.js
- jasper.js
So pets is the "parent" folder and contains the three "pet" Js files. The Js file will contain info about each pet. Here is Chico's file:
- JavaScript
- module.exports = {
- name: 'Chico',
- color: 'Brown',
- type: 'Dog'
- }
So the [chico,js] file has been set up to export info about the "Chico" pet. The other two pet files will be set up exactly the same, but with their specific pet info. And now, within our "Pets" folder, we need to create an "index" file to control the import of all the other files. So we create the new [index.js] inside of our "Pets" folder:
- pets
-
- chico.js
- jitters.js
- jasper.js
- index.js
Now, inside our new [index.js] file, we're going to require our three pet files, like so:
- JavaScript
- const chico = require (./chico);
- const jitters = require (./jitters);
- const jasper = require (./jasper);
- const allPets = [ chico, jitters, jasper ];
Now if we run our [index.js], we get:
- [
- { name: 'Chico', color: 'Brown', type: 'Dog' },
- { name: 'Jitters', color: 'Brown', type: 'Cat' },
- { name: 'Jasper', color: 'Black', type: 'Cat' }
- ]
So we can see that we have imported all three pet files as an array. Now lets go ahead and export this array from within our [index.js] file:
- JavaScript
- module.exports = allPets;
and now we can import the entire "pets" folder into our existing [app.js], by adding the following line:
- JavaScript
- const myPets = require ('./pets');
and now if we run our [app.js], we get this output:
- [
- { name: 'Chico', color: 'Brown', type: 'Dog' },
- { name: 'Jitters', color: 'Brown', type: 'Cat' },
- { name: 'Jasper', color: 'Black', type: 'Cat' }
- ]
which is the same output we got when we ran [index.js], only this time we got the output running [app.js] and it still worked because we imported the entire pets folder into [app.js].
Lets Recap:
- - We created a new pets folder.
- - Inside this folder we created three individual pet Js files.
- each file exported info on an individual pet.
- - We then created an [index.js] "controller" file that imports all three pet files
- we then added the three imports to an array, and exported that array.
- - Finally, we imported our pets folder into our [app.js] file.
and because we added the [index.js] "controller" file to our pets folder, when we import the folder, it also imports all of the individual pet files. The important point here is that most often when you import packages, they may contain many files, and they will all be controlled by an [index.js] "controller" file.