Command Line interface - Program where you type things in your computer rather than clicking - example Bash on Linux, Terminal on Mac, Command Prompt on Windows.
When working with node.js you will be working with it from the Command Line Interface , CLI .
Machine language is a language that your computer processor speaks and it is very low level.
-
Google's V8 engine is a JavaScript Engine written in C++.
-
V8 engine converts JavaScript into machine language and gives it to the processor.
-
V8 was initially made to be used in the browser, however V8 can run alone. This means that you can use the V8 code and combine it with another C++ application / program.
-
Node.js is a V8 engine with some additional C++ code added, that enables you to run JavaScript outside of the browser.
Node.js is a server technology and a JavaScript runtime environment, it is not a framework nor a programming language.
node --version
# Returns the version of the NodeJS if installed
# 12.13.0
npm --version
# Returns the version of the npm
mkdir 00-node-intro && cd 00-node-intro
touch test.js
code .
console.log("Hello Node");
node test.js
npm
is a Node Package manager that enables us to download and install different JavaScript files called 'packages' or 'modules' into our project.- Other alternative package manager is Yarn, however yarn is out of scope of this lecture and we will be using only
npm
as our package manager.
-
npm
CLI used to get the packages and it is also a server for all the packages. -
npm
registry that is the database used to keep and serve the packages. -
npm website - NPM documentation. It aslo contains information for every published package.
-
Modules aka packages are bundles of reusable code that are packaged with any of the code they depend on (dependencies).
-
One package can be build of several other packages. We call them dependencies, when one package depend on others to be installed as well.
-
When an
npm
package that we are using is updated by the package owner, we get the notification that package in our app needs an update. In this way maintaining the code is more sustainable.
Packages aka modules start from silly things such as getting the funny vaca printed on your terminal to an entire framework like React that you can get and install in your project.
To check if your npm was installed with node, in your terminal run :
npm --version
-
In order to install packages in our project we have to create a
package.json
file. -
package.json
is a manifest file that tells Node and us, what packages (aka dependencies) are installed in our project.
mkdir npm-getting-started && npm-getting-started
touch index.js
code .
While in the project folder, run the following command in the terminal:
npm init
npm init
will initiate a prompt of questions to be answered for the setup of the package.json
file.
npm install --save chalk
-
npm install
grabs the package from the npm servers -
--save
flag adds adds the package to ourpackage.json
file underdependencies
. -
If not existing already, the
node_moudles
folders is created. This is where the acctual code for all packages is stored.
If we want to have the module only for development and not for deployment, we can save it as a Development Dependency, using flag --save-dev
.
npm install --save-dev nodemon
-
npm install
will install the npm package to the node_modules folder without adding it to thepackage.json
-
npm install --save
- package(s) required by the application to run. -
npm install --save-dev
- package(s) required for development purpose.
dependencies
are 3rd-party packages that the application needs in order to run or function.
These packages will be deployed with the production version of the app and will be downloaded to the user’s browser cache whenever someone uses the app/website. In case of the back-end apps dependencies
are installed during the deployment and are required on the machine instance where the server is running.
devDependencies
are used only during the development of the app, only by the developers of the in order to build and test the app. These packages are not deployed in the production version and will not be downloaded to the browser cache of end users or to the server in case of the back-end apps.
require
is the standard module loading API for the NodeJS, which means that in order to load or export a file/module in NodeJS environment, we have to use the require
and module.exports
statements.
ES6 introduced import
and export
syntax, however for this to work on our NodeJS project we have to setup Babel
transpilation (translating) of the code from import/export
syntax to require
syntax that NodeJS uses.
Let's use our package.
To import a module and use it in our script we use the NodeJS' built inrequire()
method.
const chalk = require('chalk');
// console.log('Hello Node');
console.log(chalk.green('Hello ironhackers'));
console.log(chalk.yellow.bgRed.bold('Hello ironhackers'));
node index.js
Check out the chalk package npm
page - https://www.npmjs.com/package/chalk
Let's create a file that will contain greeting messages that we want to use.
touch greetings.js
const greetings = {
en: 'Hello Ironhackers',
es: 'Hola Ironhackers',
de: 'Hallo Ironhackers',
fr: 'Salut Ironhackers',
it: 'Ciao Ironhackers'
};
/*
We export the greetings object, in order to be able to
require it from another file.
*/
module.exports = greetings;
We can now load the exported module/file in our test.js
file
const chalk = require('chalk');
const greet = require('./greetings'); // .js extension is not compulsory
/*
greet = {
en: 'Hello Ironhackers',
es: 'Hola Ironhackers',
de: 'Hallo Ironhackers',
fr: 'Salut Ironhackers',
it: 'Ciao Ironhackers'
}
*/
console.log( chalk.whiteBright.bgBlue.bold(greet.en) );
console.log( chalk.yellow.bgRed.bold(greet.es) );
console.log( chalk.bgYellowBright(greet.de) );
console.log(
chalk.whiteBright.bgBlueBright(greet.fr.split(' ')[0]),
chalk.whiteBright.bgRed(greet.fr.split(' ')[1]),
);
console.log(
chalk.whiteBright.bgRed(greet.it.split(' ')[0]),
chalk.whiteBright.bgGreen(greet.it.split(' ')[1]),
);
Dev Dependencies vs Dependencies (5min read)