NPM Flashcards
What are the two ways to install node packages?
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 do we use an npm package?
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 do we specify only a single function to import from a package with multiple independent files?
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 do we specify only a single function to import from a package without multiple independent files?
const chunk = require(‘lodash’).chunk;
console.log(chunk([1, 2, 3, 4, 5, 6, 7, 8], 2))
What does the package.json file do?
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 do we add our dependencies to our package.json file?
“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.
What does package-lock.json do? How do we create it?
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.
What are the two ways to add a new dependency?
- Directly add the dependency to
package.json
- Use
npm install
How do we save dependences that we only need in development?
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 do we uninstall a dependency?
npm uninstall lodash
Why do we generally want to install packages locally?
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.
What is transpilation?
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
What are npm scripts?
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.
What is the most widely used JS transpiler?
Babel
What is the syntax for an npm script?
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”
}