NPM Module #43 Flashcards

1
Q

How would you install a package?

A

npm install package-name

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

How would you make sure dependencies are added to the package.json file?

A

npm install package-name –save OR
npm install package-name –save-dev which adds the package to package.json devDependencies

*Both are important for compiling a bunch of things later

The difference is mainly that devDependencies are usually development tools, like a testing library, while dependencies are bundled with the app in production.

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

What’s another perhaps quicker way of adding devDependencies to package.json?

A

Simply type the -D flag instead of –save-dev

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

Without deliberately specifying production, NPM assumes a dev build. How would you avoid this if you were intalling into production?

A

By setting the —production flag.

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

How would you globally install a package and why would you want to do that vs. a local project?

A

Use the -g flag
Typically this is for other command line tools. A package should be installed globally when it provides an executable command that you run from the shell (CLI), and it’s reused across projects. vue/cli wordpress cli etc

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

What are the rules for NPM versioning?

A

The Semantic Versioning concept is simple: all versions have 3 digits: x.y.z.

the first digit is the major version
the second digit is the minor version
the third digit is the patch version
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you return a list of global dependencies?

A

npm list -g –depth 0

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

What are the various ways to uninstall a package?

A

Using the -S flag, or –save, this operation will also remove the reference in the package.json file.

If the package was a development dependency, listed in the devDependencies of the package.json file, you must use the -D / –save-dev flag to remove it from the file:

npm uninstall -S
npm uninstall -D

If the package is installed globally, you need to add the -g / –global flag:

npm uninstall -g `

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

What is stored in the package.json file?

A

It’s a central repository of configuration for tools, for example. It’s also where npm store the names and versions of the package it installed. The list is below.

The file structure
Properties breakdown

    name
    author
    contributors
    bugs
    homepage
    version
    license
    keywords
    description
    repository
    main
    private
    scripts
    dependencies
    devDependencies
    engines
    browserslist
    Command-specific properties
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are the constraints and rules for setting the name in a package.json file?

A

The name must be less than 214 characters, must not have spaces, it can only contain lowercase letters, hyphens (-) or underscores (_).

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

How would you structure the GIT information in the package.json?

A

“repository”: “github:flaviocopes/testing”,

or you can explicitly set the version control system:

“repository”: {
“type”: “git”,
“url”: “https://github.com/flaviocopes/testing.git”
}

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

If privacy is set to “false” in package.json, what happens?

A

The package is published to NPM. Make sure that it is set to “true” unless you intend to publish.

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

What is the command to install devDependencies? Why is this different from dependencies?

A

stall –only=dev

This inserts the dependencies required for development, not the final production build.

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

What is the package-lock.json file?

A

The goal of the file is to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers.

The package-lock.json sets your currently installed version of each package in stone, and npm will use those exact versions when running npm install.

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

How can you make updates to globally installed packages?

A

npm install -g npm-check-updates

Then run ncu -u

This will upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version.

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

Where are packages installed by default?

A

in the current file tree, under the node_modules subfolder. Short answer, locally.

17
Q

Where exactly are files added when using the -g global flag?

A

Globally yes, but in the npm root directory of your computer.

The npm root -g command will tell you where that exact location is on your machine.

On macOS or Linux this location could be /usr/local/lib/node_modules. On Windows it could be C:\Users\YOU\AppData\Roaming\npm\node_modules

**If you use nvm to manage Node.js versions, however, that location would differ.

I for example use nvm and my packages location was shown as /Users/tomphillips/.nvm/versions/node/v15.14.0/lib/node_modules