npm Flashcards Preview

Web Development > npm > Flashcards

Flashcards in npm Deck (20)
Loading flashcards...
1

What is npm?

npm(Node Package Manager) is the world's largest software registry. Open source developers from every continent use npm to share and borrow packages, and many organizations use npm to manage private development as well.

npm consists of three distinct components:

1. The Website: Use the website to discover packages, set up profiles, and manage other aspects of your npm experience. For example, you can set up organizations to manage access to public or private packages.

2. The Command Line Interface (CLI): The CLI runs from a terminal, and is how most developers interact with npm.

3. The Registry: The registry is a large public database of JavaScript software and the meta-information surrounding it.

2

What is a package?

A directory with one or more files of reusable code in it, and also has a file called package.json with some meta-data about this package.

If it has package.json, it is a package.

3

How can you create a package.json with npm?

~/repos/c0621-code-solutions/npm-intro (npm-intro)
λ npm init --yes

4

What is a dependency and how do you add one to a package?

A dependency is another package that your package needs in order to work.

You can add one to a package by:
Ex:
npm install jquery

5

What happens when you add a dependency to a package with npm?

It downloads the package so it can be used.
The property dependencies get added to package.json:

{
"name": "npm-intro",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.6.0"
}
}

6

How do you add express to your package dependencies?

npm install express

7

What Express application method starts the server and binds it to a network PORT?

listen( )

8

How do you mount a middleware with an Express application?

Using the use/get method.

Examples:

app.use(function (req, res, next) {
res.send('Hello World')
} )

app.use(function (req, res, next) {
console.log('Time: %d', Date.now( ))
next( )
} )

It happens when I request is received in the server.
Event listener for requests.

9

Which objects does an Express application pass to your middleware to manage the request/response lifecycle of the server?

req and res.

request and response.

req is a data-model for the request message:
GET / HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: 192.168.1.40:1337
User-Agent: HTTPie/1.0.3

10

What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?

application/json

11

What does the express.json( ) middleware do and when would you need it?

express.json( ) is a method inbuilt in express to recognize the incoming Request Object as a JSON Object. This method is called as a middleware in your application using the code: app.use(express.json( ));

If the request includes JSON, use it.

12

What is the significance of an HTTP request's method?

It defines what is to be done with the data.

The method name is just for semantics.

13

What is Webpack?

It's a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.

Bundles modules.

14

How do you add a devDependency to a package?

npm install --save-dev webpack

or

npm i -D webpack

15

What is an NPM script?

An npm script is a convenient way to bundle common shell commands for your project.

Command line shortcuts.

16

How do you execute Webpack with npm run?

Add a script in package.json

Ex:

"scripts": {
"build": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},

17

What kind of modules can Webpack support?

ECMAScript modules. CommonJS modules. AMD modules.

18

What does express.static( ) return?

It returns a middleware function. Programmed to look in directory and sort.

19

What is the local __dirname variable in a Node.js module?

The directory name of the current module. This is the same as the path.dirname( ) of the __filename.

__dirname is an absolute path.

20

What does the join( ) method of Node's path module do?

The path.join( ) method joins all given path segments together using the platform-specific separator as a delimiter( / ), then normalizes the resulting path.