npm and package.json
Homework
- npm
- Read: An Absolute Beginner’s Guide to Using NPM
- Read: Anatomy of a
package.json
File on Digital Ocean - Reference: Node Documentation
- Tools
- Dates
Morning reflection
Housekeeping
- Announced tomorrow:
- Draft - Assignments 4 & 5
- Draft - Final Project
- Groups for Final Project
1. The http
builtin module
Learning Objectives
- Create a simple
http
server using Node. - Understand when code is executed:
- on initial script execution;
- when a
request
event occurs.
- Start a server on port
8080
.
Materials
Key Takeaways
- You have to restart the server before it loads new changes.
- The
http
module allows us to create servers without installing dependencies but it requires a lot of work to setup the features that a production server requires (404
handling, reading HTML files from the file system, etc). - Starting tomorrow, we’ll use
npm
to install theexpress
framework, which simplifies the creation and management of web servers.
2. An introduction to npm
Learning Objectives
- Define common terminology for
npm
projects. - Summarize the history of
npm
. - Define the problem that
npm
solves. - Understand semantic versioning and
update
rules.~1.2.3
: Auto-update patch releases (1.2.x
)^1.2.3
: Auto-update minor releases (1.x
)
Materials
- Gist: An introduction to
npm
Key Takeaways
npm
is great but it has a weakness: it’s hosted by a single registry that can go offline. When this happens, it’s impossible toinstall
andupdate
dependencies.
3. Creating npm
projects
Learning Objectives
- Recognize the presence/absence of an
npm
project. - Understand the [anatomy of a
package.json
file], specifically:name
: name of the projectmain
: Entry script filename (i.e.app.js
,server.js
, etc)scripts
: Command line shortcutsdependencies
: Project production dependenciesdevDependencies
: Project Development dependencies
- Initialize a new
npm
project
Materials
Key Takeaways
npm
projects are usually in the same root directory as agit
repository.- Don’t initialize projects within projects.
- Add
node_modules
to your.gitignore
file to avoid storing needless files in your repository. - Commit
package.json
andpackage-lock.json
to your repository. - Don’t manually edit
package-lock.json
. This is maintained bynpm
to lock dependencies to specific versions/ranges. - Update
package.json
by the command line (or other tool) unless you’re comfortable editing the file manually.
4. Installing new dependencies
Learning Objectives
- Evaluate an
npm
package vs alternatives. - Install a package from the
npm
registry. - Import a package into a Node app.
Materials
- Activities:
Key Takeaways
momentjs
is an industry favourite for handling dates but even the maintainers recommend more modern alternatives.- There are many
npm
options for date handling. Here’s a handy breakdown of the most popular ones.
Open lab-time
Activities
- Using the internet and the simple
http
server created earlier today, figure out how to send the requested URL as a response instead of “Hello World”. - Output a fancy date to a web site.
- Create a simple
http
server; - Initialize a new
npm
project; - Install
dayjs
as a dependency; - Load
dayjs
into thehttp
script; - Add a fancy date to the “Hello World” message sent by the server.
- Create a simple
- Practice downloading dependencies:
- Complete the above activity and create an
http
server withdayjs
as a dependency. - Delete your
node_modules
directory. -
Try starting your server. You should get the following error:
Error: Cannot find module 'module-name'
-
Download your dependencies:
$ npm install
- Try starting the server again and everything should be amazing.
- Complete the above activity and create an
- Confirm you are ignoring the
node_modules
directory in Git:- Create a new
npm
project with an installed dependency of your choice. - Initialize a new git repo.
- Create a remote repo for this project on GitHub.
- Push your local code to the remote.
- Confirm that the remote repo does not have a
node_modules
directory.
- Create a new
- Prep for tomorrow: create a new
npm
project and installexpress
as a dependency.
Tony’s goals for Lab-Time
Dailies
- Submit today’s Codepen/repo/gist to the Dailies section (in Assessments) in Brightspace.