NodeJS Flashcards

1
Q

What is NodeJS?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do people build with Node?

A

Native apps not on the browser such as VSCode. Web servers, command line tools, video games, and more.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the Node Repl?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are some things NodeJs has that JS doesn’t and other way around.

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the global scope(Holds all functions, alerts, etc.) in Node?

A

It’s called Global.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How would we run a script(.js) file with Node?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is process?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is process.argv?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do node modules work?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does Node work with the index.js file?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is NPM?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why do we want to install packages locally in the node modules?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would we use the Jokes package?

A
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); 
});
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is package.json?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Do we include node modules folder when we upload our work or have others work with it?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

If we’re using someone’s project and it has dependencies in the package.json, how can we continue to run the project and not have to manually install each dependency?

A

We go into the directory that holds the project we directed and just do npm install. This will look at package.json and install all dependencies. This gives us a node modules directory.