NPM packages Flashcards
What is the difference between dependencies, devDependencies and global installs?
devDependencies are used when we are developing the project, while dependencies are used within the application code.
In order to save a devDependency we need to
npm install package-name –save-dev
Global installs are available anywhere on our machine. A package should be installed globally, if it provides an executable command that can be run from the CLI.
What does nodemon package do?
Nodemon is a package, that we use to automatically restart server upon saving changes in the code.
It should be installed in the –save-dev
devDependencies.
nodemon index.js
What is slugify?
Slugify is a package that can help us to make more readable urls out of names.
Slug - is the last part of the url that contains the string that identifies the resource the website is displaying.
What is the meaning of semantic version notation of versions of npm packages?
Usually, npm package version number is expressed with these three numbers: “^1.18.11”;
The first number, is the major version (huge new release, which can have breaking changes),
second number is the minor version (introduces new features, but not breaking changes - always are backward compatible)
third one is patch version (only intended to fix bugs).
- will accept all of the versions (is not safe, because, the versions might contain breaking changes)
^ will accept patch and minor releases
~ will accept only patch releases (is safer)
How can we update and delete packages in NPM?
- Check if there are any outdated packages (should give us a table with all packages that are outdated):
npm outdated - npm update slugify
(cares about ^ or ~).
We can install an older version of a package by:
npm install slugify@1.0.0
To delete a package, we need to
npm uninstall slugify
What is the procedure to install packages in your project?
- Node package manager automatically comes with installment of node.js.
- A good habit when working on a new project that will be using packages is to
npm init
in the project directory. This will create a package.json file that tracks our devDependencies. - To install a package we need to type in vs code terminal
npm install
command.--save-dev
` (and save the dependencies in the package.json file.
What is the procedure to install babelJS?
To install babel inside our project directory we need to have a node environment.
- init the project package.json with
npm init
command. - install packages:
npm install @babel/core @babel/cli --save-dev
babel cli - command line interface, to use babel in cmd. - Install babel preset:
npm install @babel/preset-env
- Create a .babelrc file and store the presets in an object. Then, when we run babel, the program will know which preset to run, because it knows about .babelrc file.
{
“presets”: [“@babel/preset-env”]
}
How can we run babelJS?
In order to run babel compiler we need to run a command in CLI.
In the command, we need to:
- Locate the babel bin in node_modules folder.
- select the file that we want to compile
- specify the output destination and name of the file.
node_modules/.bin/babel ./scripts/before.js -o ./scripts/after.js
Installation of packages used in projects.
Whenever we send the project to another developer or upload to github repository, we do not include node_modules folder.
node_modules is the location of all packages scripts and simply that is a lot of code.
However all of the dependencies are stored in package.json file. When we upload a project, we just need to upload the package.json file.
Then, when the project is downloaded, simple command npm install
will install all the dependencies located in package.json.
What is the purpose of using NPM Scripts and Watching files?
NPM Scripts allow us to automate he process of bundling/compiling files.
In our project directory we could have a src and dist folders
Mode LastWriteTime Length Name
—- ————- —— —-
d—– 9/22/2020 11:14 AM dist
d—– 9/22/2020 11:02 AM node_modules
d—– 9/22/2020 11:14 AM src
-a—- 9/22/2020 10:42 AM 42 .babelrc
-a—- 9/22/2020 10:49 AM 123351 package-lock.json
-a—- 9/22/2020 11:21 AM 370 package.json
src, being the folder for all scripts with modern features.
dist, folder thats being uploaded on the server (with index.html etc).
In our package.json, we could create a script
“scripts”: {
“babel”: “node_modules/.bin/babel ./src/index.js (-w) -o ./dist/assets/bundle.js”
}
This will allow us to run command
npm run babel. And bundle the js file. We could additionally add a -w modifier, that will watch any changes in the source code in index.js and automatically comile it into bundle.
Installation of webpack
How to set up a webpack.config.js file?
const path = require('path'); // require path module located in node_modules
module.exports = {
entry: ‘./src/index.js’,
output: {
path: path.resolve(__dirname, ‘dist/assets’),
filename: ‘bundle.js’
}
};
npm install webpack webpack-cli --save-dev
How can you set up webpack dev server?
- Install package
npm install webpack-dev-server
- add configuration to webpack.config.js file
devServer: {
contentBase: path.resolve(__dirname, ‘dist’),
publicPath: ‘/assets/’
} - add a script to build run dev server.
“dev-server”: “webpack-dev-server –mode development”
(–mode production to run for producion).
How do you set up CSS loaders?
1. Install
npm install css-loader style-loader –save-dev
What is morgan?
Morgan is a logging middleware. It logs HTTP requests to the console. It used to be a built in express.
In order to include morgan in our express app, we need to install it by npm.
and
app.use(morgan(‘dev’))
morgan(‘dev’) returns a middleware function that the app uses.
What is the purpose of dotenv package?
dotenv is a standard package to set up environmental variables (config.env). It reads the variables from the file and saves them as environment variables, so they are accessible in process.env.nameOfVariable
How do you set up prettier together with ESLint extensions?
Bunch of dependencies:
npm install eslint prettier eslint-config-prettier eslint-plugin-prettier eslint-config-airbnb eslint-plugin-node eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react –save-dev
What is mongoose?
Mongoose is an Object Data Modeling (ODM) Library for MongoDB and Node.js, providing a higher level of abstraction.
- schemas to model data and relationshipts
- easy data validation
- simple query api
- middleware