Hey there DEV.to community!
I remember the days I was starting to work with Node.js and as my first project I wrote a simple payment app in Node.js and it was really fun! Although I enjoyed writing that app I was struggling with some aspects that they were new for me and I wasn't familiar with them, so here I'm sharing these steps if you are also moving to Node.js and trying to understand what is going on.
As I'm writing this post it is almost 7 hours to the new year here in Iran, called Nowruz. So, happy new year! xD
(BTW it's been almost a month that I'm at home and I didn't go out due to coronavirus. I miss my friends :(((, I highly suggest you stay at home if you are living in a country/city that there are cases of coronavirus! )
package.json
package.json
is a file that you will be working with a lot if you are writing a project using Node.js doesn't matter if it is a server-side project or you are using Node.js to pre-process your front-end.
package.json
is a file that holds some data about your app like its name, version, author, repository and etc but most importantly your app's dependencies.
Dependencies are simply libraries your app is going to use and are handled by a package manager (gonna explain this one as well here in this post).
So this file is not a scary thing it is only a file holding your apps data.
This is how a package.json
file usually looks like:
Package manager
Package managers are really useful tools that they exist for almost every programming language. In this case, when you are working with Node.js you will probably be using npm
or yarn
(pnpm
is also available).
What these tools do is simply installing and managing your dependencies that are defined in your package.json
(other files might be used as well in other package managers like yarn
). Each library can have its own dependencies that these package managers are going to install them as well so you can be sure about them!
Check NPM and Yarn's official websites for more information.
Modules
Modules are the codes that can be used again and again and again (libraries :|)! So when you hear the word module
don't be afraid it is as same as libraries in other languages.
Now there are two ways of calling a module in Node.js, you can either pass the name of your module which is going to be called from a folder called node_modules
or you can pass the absolute path to your module.
const myModule = require('myModule')
const myAbsoluteModule = require('path/to/your/module.js') // You can omit `.js` extension
node_modules
node_modules
is a folder where all of your libraries live so be kind to it! Any module that is installed along with all its dependencies is stored here and when you are requiring them in your code they are getting called from here as mentioned above.
Keep in mind that you should ignore this folder when committing a change to your git repository or when uploading your project to deploy it, the reason is that this folder is usually a heavy one (sometimes around 1 GB believe me or not) and it's going to take a long time and also going to mess your git repository. So what the solution for this? Remember the section above about the package.json
file that was holding your dependencies' names? So when deploying your project you only need your package.json
file and a package manager to install all that is needed without having to upload all your dependencies again and again.
Built-in modules
There are some modules which are built-in, that to be explained simply means you don't need to install them and are already included in your Node.js setup. A famous built-in library is fs
which stands for File System.
The only need to require it in order to use it:
const fs = require('fs')
Bonus tips
These are some bonus tips that are not directly related to Node.js but might help you.
Do you have a PHP background?
If you are from a PHP background like I was when learning Node.js you might feel some aspects really bizarre and strange but they will make more sense to you as you go forward.
PHP was built to be a template engine originally but it grew and become a whole programming language so you can write PHP inside your HTML (not encouraged though) and this is not possible in Node.js! You have to use a template engine in order to be able to inject your data into HTML files and show them to your users. Some of the most popular template engines are EJS, Mustache and Pug.
Babel
You might hear the name Babel a lot. Babel is a powerful tool/library used for compiling new generation JavaScript down to the older generations of JavaScript (ES version) so they can understand what you mean in case you are using Node.js to pre-process your front-end.
Check out these two codes:
import moment from 'moment'
const time = moment()
function sum(x = 2, y = 5) {
return x + y
}
Babel compiles this code down to this:
"use strict";
var _moment = _interopRequireDefault(require("moment"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var time = (0, _moment.default)();
function sum() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
return x + y;
}
Or a better example can be the classes that are relatively new to JavaScript:
class Pet {
constructor(name) {
this.name = name
}
thisPetsName() {
return this.name
}
}
So this gets compiled to this:
"use strict";
function _instanceof(left, right) { if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) { return !!right[Symbol.hasInstance](left); } else { return left instanceof right; } }
function _classCallCheck(instance, Constructor) { if (!_instanceof(instance, Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var Pet = /*#__PURE__*/function () {
function Pet(name) {
_classCallCheck(this, Pet);
this.name = name;
}
_createClass(Pet, [{
key: "thisPetsName",
value: function thisPetsName() {
return this.name;
}
}]);
return Pet;
}();
Now you can see how powerful Babel is!
I hope you enjoyed this article!
Please tell me if I'm mistaken about any points or if you want me to add some part that might help others!
Top comments (0)