NodeJS Flashcards
What is NodeJS?
JavaScript runtime, pretty much a container where a program in javascript can be executed but outside of any browser. The v8 engine parses and runs it in nodejs instead of the browser.
What do people build with Node?
Native apps not on the browser such as VSCode. Web servers, command line tools, video games, and more.
What is the Node Repl?
Read Evaluate Print Loop. The JavaScript console in our browser is a Repl. We can type some code in and it will read it, evaluate it, and then print it back and then loops.
We use the Node repl to debug or play around with new features. We can type JavaScript in here and we can think of it as the node equivalent of the JavaScript console in the Chrome.
What are some things NodeJs has that JS doesn’t and other way around.
Node comes with a bunch of built in modules that don’t exist in the browser. These help interact with operating system and files/folders.
Because Node doesn’t run on the browser, we don’t have access to browser stuff like the window, document, and DOM API’s
What is the global scope(Holds all functions, alerts, etc.) in Node?
It’s called Global.
How would we run a script(.js) file with Node?
After creating the file in node terminal using touch at start, we can use node firstScript.js. Now we see this in the node terminal. When doing this you need to be in the folder where the file is located or you need to reference the whole path in the terminal.
Node on its own opens repl and node with filename executes the script using node.
What is process?
Process is an object that’s available anytime we use node and it contains information about and methods that provide control over the current Nodejs process.
A method example would be process.cwd(). cwd is current working directory which gives us the path to where we’re currently running node.
What is process.argv?
Returns an array containing the command line argument passed when node process was launched. First element will be process.execPath where the node executable is. The second value is always going to be the file that we’re executing. Any remaining elements will be additional command line arguments.
So if we console.log(process.argv) we get
/usr/local/cin/node
/Users/asdlfkj/Code/File Name
And now if we write anything else on the command line it gets added as an argument and we see it printed out
puppies
chickens
hello
How do node modules work?
const math = require(‘./Whatever the file name is’)
This will give us an empty object as this is how node modules work. When we create a file, like Math.js, content of the file is not automatically available everywhere else when we require that file. If we have 2 scrips in a index.html, they’re both going to load their stuff and second has access to whatever is in the first one. Not the case here.
Solution? need to do module.exports which is an object by default.
module. exports.add = add;
module. exports.PI = PI;
How does Node work with the index.js file?
It’s the main file of a directory, when we require an entire directory Node is going to look for an index file and whatever that file exports is what the directory will export.
What is NPM?
There are 2 parts. 1 it’s a whole bunch of packages written by others we can use. There are packages for React, Express, etc.
The second piece is it’s a command line tool that we can use to easily install and manage those packages
Why do we want to install packages locally in the node modules?
We don’t want access to every package ever in every file because we may want different versions for different projects. If we work with Express or React there are significant differences between versions.
How would we use the Jokes package?
We first install with npm install give-me-a-joke and it'll look for package in npm registry with same name. A folder is then added to our project called Node Modules. We use package by using require. const jokes = require("give-me-a-joke"). If we were to console log jokes we'd see 4 different methods that have been exported from give-me-a-joke. We can use it with the callback function provided in the npm docs for this package. jokes.getRandomDadJoke(function (joke) { console.log(joke); });
What is package.json?
Will be in every node app that’s not required but we’d want it there and it contains meta data or info about this project/package/application. We’ll see description, name, version, etc. We care about dependencies.
Usually the we create this package.json file is with an npm command. We do this with npm init. Now we won’t see warning whenever we install a package. Note that this package.json we create with npm init is separate and different than the one found in the packages itself. But the name and version will show up in the dependencies in the package.json we created. So our package.json acts as a record of everything that we’re using. Usually in the root directory.
Do we include node modules folder when we upload our work or have others work with it?
We can but generally no as node modules can take up a lot of space. In that case stick to download code, navigate to folder in command line and do npm install.