npm Flashcards
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:
- 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.
- The Command Line Interface (CLI): The CLI runs from a terminal, and is how most developers interact with npm.
- The Registry: The registry is a large public database of JavaScript software and the meta-information surrounding it.
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.
How can you create a package.json with npm?
~/repos/c0621-code-solutions/npm-intro (npm-intro)
λ npm init –yes
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
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" } }
How do you add express to your package dependencies?
npm install express
What Express application method starts the server and binds it to a network PORT?
listen( )
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.
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
What is the appropriate Content-Type header for HTTP messages that contain JSON in their bodies?
application/json
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.
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.
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.
How do you add a devDependency to a package?
npm install –save-dev webpack
or
npm i -D webpack
What is an NPM script?
An npm script is a convenient way to bundle common shell commands for your project.
Command line shortcuts.