NPM Flashcards

1
Q

What are the two ways to install node packages?

A

You can install Node packages in two ways: locally and globally. Packages needed by an application or project are commonly installed locally inside the project directory. Typically, these packages are often the kind that we import into our program. To install a package locally, use thenpm installcommand without the–globaloption.

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

how do we use an npm package?

A

const _ = require(‘lodash’);

console.log(_.chunk([1, 2, 3, 4, 5, 6, 7, 8], 2));
// logs [[1, 2], [3, 4], [5, 6], [7, 8]]

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

How do we specify only a single function to import from a package with multiple independent files?

A

const chunk = require(‘lodash/chunk’);

console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8], 2));
// logs [[1, 2], [3, 4], [5, 6], [7, 8]]

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

How do we specify only a single function to import from a package without multiple independent files?

A

const chunk = require(‘lodash’).chunk;

console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8], 2))

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

What does the package.json file do?

A

The package.json file lists all the packages that your project needs, together with the versions of each package. The file also provides a convenient place to store some other configuration settings.

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

how do we add our dependencies to our package.json file?

A

“dependencies”: {
“express”: “4”,
“http-errors”: “1”,
“morgan”: “1.9”
},

Once we’ve saved this file we can run npm install to install the required dependencies. This creates our package-lock.json file.

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

What does package-lock.json do? How do we create it?

A

Thepackage-lock.jsonfile shows the precise versions of the packages that npm installed. The next time we runnpm installit’ll look atpackage-lock.jsonand install the specific versions specified there. This is an auto generated file.

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

What are the two ways to add a new dependency?

A
  1. Directly add the dependency topackage.json
  2. Usenpm install
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do we save dependences that we only need in development?

A

if your project only needs a package during development, you can save it only to the dev environment with the devDependenciesproperty.

npm install eslint –save-dev

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

how do we uninstall a dependency?

A

npm uninstall lodash

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

Why do we generally want to install packages locally?

A

As a general rule, you should install almost all of your packages locally in your project’snode_modulesdirectory. We recommend this practice highly. It ensures that projects that need specific versions of packages have them locally available.

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

What is transpilation?

A

Transpilationis the process of converting source code written in one language into another language with a similar level of abstraction to the original code. In the JavaScript world, it often means taking code written in a superset of JavaScript and rewriting it as plain JavaScript

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

What are npm scripts?

A

Scripts automate repetitive tasks such as building your project, minifying files, and deleting temporary files and folders. To use npm scripts, you first need to define them in thepackage.jsonfile with the”scripts”object.

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

What is the most widely used JS transpiler?

A

Babel

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

What is the syntax for an npm script?

A

The”scripts”object takes a series of key/value pairs in which each key is the name of the script, and the value is the script you want to run.

“scripts”: {
“test”: “echo "Error: no test specified" && exit 1”,
“foo”: “echo ‘How do you do?’”,
“babel”: “npx babel lib –out-dir dist –presets=@babel/preset-env”
}

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

how do we remove unnecessary dependencies

A
  1. remove them from package.json
  2. runnpm pruneto remove them fromnode_modulesas well.
16
Q

What are the steps for publishing an npm module?

A
  1. Create apackage.jsonfile.
  2. Provide values for thename,version, andmainfields:
    • nameis the name of your package.
    • versionis the initial module version.
    • mainis the name of the file that Node will load when someone imports your package.
  3. Publish your node package.