📘
NavDoc by Bash School
GithubContact
📘
NavDoc by Bash School
  • 🎓Introduction
  • 🐢Getting Started
  • ⚡Changelog
  • 👨‍🚀Maintainers
  • 🛣️Roadmap
  • Fundamentals
    • The Internet
      • Introduction
      • What is a URL
      • What is a port
      • The DNS protocol
      • The TCP protocol
      • The UDP protocol
      • The Web
      • The HTTP protocol
      • Hyperlinks
      • What is a Web browser
      • What is a Web server
    • HTML
      • Your first HTML page
      • Text tags
      • Attributes
      • Links
      • Images
      • Lists
      • Head Tags
      • Container tags
    • CSS
      • Introduction
      • Colors
      • selectors
      • Cascade
      • Specificity
      • Units
      • Advanced selectors
      • Typography
      • The box model
      • The display property
      • Responsive design
  • JavaScript
    • Basics
      • Introduction
      • Literals , Identifiers, Variables
      • Comments
      • The difference between let, const and var
      • Types
      • Operators and expressions
      • Arithmetic operators
      • The assignment operator
      • Operators precedence
      • Strings
      • Numbers
      • Semicolons, white space and sensitivity
      • Arrays
      • Conditionals
      • Loops
      • Functions
      • Objects
      • Arrays + functions
      • OOPS
      • Asynchronous
      • Scope, hoisting, event loop
      • ES Modules
      • Errors and exceptions
      • Built-in objects
        • The global object
        • Object properties
        • Number
        • String
        • Math
        • JSON
        • Date
        • Intl
        • Set and Map
      • More operators
    • Nodejs
      • Getting Started
      • Installation
      • Hello World in Node
      • Modules
      • Packages
      • File Handling
      • HTTP Request
      • Processing Files
      • HTTP
    • Express.js
      • Getting Started
      • Middleware
      • Serve Static Assets
      • How to Send Files to the Client
      • Sessions
      • Validate Input
      • Sanitizing Data
      • Forms
      • File Uploads
    • React
      • Setting up a React project with Vite
      • React Components
      • Introduction to JSX
      • Using JSX to compose UI
      • The difference between JSX and HTML
      • Embedding JavaScript in JSX
      • Handling user events
      • Managing state
      • Component props
      • Data flow
      • Lifecycle events
      • Managing forms in React
      • Install the React Developer Tools
      • Installing Tailwind CSS in a React app
      • Build a counter in React
    • TypeScript
      • Key Benefits
      • Types of Languages
      • The Need for TypeScript
      • What is TypeScript?
      • The tsc Compiler
      • Basic Types in TypeScript
      • tsconfig
      • Interfaces
      • Types
      • Arrays in TypeScript
      • Enums
      • Exporting and importing
    • MongoDB
      • SQL vs. NoSQL Databases
      • Installing MongoDB
      • MongoDB Databases and Collections
      • Working with Documents
      • MongoDB Operators
      • Sorting, Indexing & Searching
      • Built-in Methods
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. JavaScript
  2. Nodejs

Packages

It is not productive to create everything from scratch. To increase productivity, NodeJS provides a mechanism to re-use previously written code. Such code is called a package. Packages can be installed via a commandline utility called NPM.

Packages can be installed either Global or local. Global packages are installed on the machine and can be used from the command line or by all Node apps from that single installation. Local packages are installed with each Node app and cannot be used by other Node apps.

When a Node app uses a package, it is called a dependency. This means that the Node app depends on the package in order to function properly. Packages can also use other packages as dependencies. Packages have version numbers. A version number indicates a specific iteration of code or features that the package supports. When a Node app depends on a specific version of a package, it avoids protentional problems and conflicts that might arise if there was no versioning in place. A conflict is when the Node app expects a certain feature or behavior that does not exist, or behaves differently than expected.

To install a package:

NodeJS provides a utility called npm that manages packages. With it you can install and remove packages easily.

Each application should have a package.json file in its root directory. This file does not exist by default and must be created. To create it, simply type:

npm init at the application root directory and follow the prompts. To add a NodeJS package, simply type:

npm install <name> --save at the application root directory.

You can also remove a package via:

npm remove <name> at the application root directory.

When you get a project from GitHub the first time, it will usually contain the package.json file already. But the packages will usually not be included with the git repository. The packages will be listed in the package.json file. To install all the package dependencies, simply type:

npm intall at the application root directory.

Some packages will behave more like applications and others more like functionality for YOUR Node app. Those that behave more like applications (or utilities) must be installed globally and those that provide functionality as a part of YOUR Node app must be installed locally. To install a global package, simply type:

npm install -g <name> anywhere.

Exercises:

  • Create a new Node app. Add a package.json file

  • What is the purpose of the main key/value in the package.json file?

  • Install at least one package to the project

  • What happens if you omit --save when installing a package?

  • Install at least one package that is specific to the dev dependencies (HINT: testing usually will be dev specific (as in development, vs deployed to the end users and ready to use))

  • Remove at least one package from the project

    • Validate that package.json reflects the change. If not, figure out why and try again

  • Install a specific version of a package to the project

  • What directory does NPM install local packages to?

  • What happens if you install a package at a different directory level than the application root directory?

  • How can you avoid uploading package files to github? (HINT: git.ignore... but how, specifically?)

  • Install a global package (Nodemon??)

  • Can a global package be a dependency to your Node app? Explain why or why not?

  • Launch your NodeJS app using the dev configuration.

PreviousModulesNextFile Handling

Last updated 1 year ago

Was this helpful?