npmfor the rest of Bootcamp to build more complex apps that rely on 3rd-party packages.
npm initinitialises the NPM project, and the
-yflag sets our answers to
yesfor all of the setup questions.
package.jsonspecifies all configuration for our Node project managed by NPM. It operates similarly to the
.gitfolder in our Git repos that configures everything Git-related for each repo.
package.jsonfile in VSCode.
importsyntax to import Node modules. Before Node version 12 the default syntax was
require, and much documentation online still references the old
require, but we will use
importwhere we can for consistency. To use
importwith Node.js we will need to add the
"type": "module"key-value pair to
export defaultfrom the imported module. There are no named exports with
npm install, our NPM project will now list the installed package as a "dependency", i.e. a package that our app depends on to run properly. NPM will also have installed all of our package's dependencies, and their dependencies ad infinitum. NPM packages almost always depend on other NPM packages, because nobody builds apps from scratch. For example, here is the dependency graph for the
eslintNPM package that we have been using since Coding Basics.
npm install, NPM adds a new key in
package.jsoncalled "dependencies" if it doesn't exist already. NPM installs the most recent version of the package as a dependency, and lists the package name and version number in the "dependencies" object in
package.jsonso that other copies of this repo can also retrieve the same package version. If we need a specific package version that is not the latest, we can specify it when we run
npm install, NPM creates a file called
package-lock.json(if it doesn't exist already) that records version numbers of all dependencies of the target package (and the dependencies of those packages, ad infinitum), so that if we reproduced this repo on another computer, we would use the same dependency versions.
package-lock.jsonis managed by NPM and we should never have to manually edit
npm install, NPM creates a folder called
node_modules(if it doesn't exist already) with the package we installed and all of the packages it depends on. You may notice that there are many more folders in
node_modulesthan packages we installed explicitly, because
node_modulescontains a folder for every dependency of every dependency. Similar to
node_modulesis managed by NPM and we should never need to manually modify code within
package-lock.jsonto Git so that other computers and team members can access the same versions we are using, eliminating bugs due to version mismatches.
node_modulesto Git, because
node_modulescan get quite large and everything in
node_modulescan be installed on an Internet-connected machine by running
npm installin the project root folder where
package.jsonis. Committing a large, redundant set of files like
node_moduleswill unnecessarily slow down Git operations, especially pushing and pulling from GitHub.
.gitignore, and that this file often has a line that says
node_modules. This file tells Git which files or folders to ignore when detecting changes. In this case, we have set
.gitignoreto ignore the
node_modulesfolder so that we never commit
node_modulesto the repo. Read more about
package.jsoncalled "devDependencies". dev or development dependencies are dependencies used purely for development purposes, which developers do not want to be included in the application package deployed to end users. The primary reason for this is to reduce the size of the app for faster load times.
index.jsfile in the
cowspackage we installed in
node_modules. Are you able to understand the code?