Q1. How do you install a package locally?
Run npm install in your project directory. This installs the package in the node_modules folder and adds it to the dependencies list in package.json (if you use --save flag, which is default now). Example: npm install express. Local packages are only available within that project.
Q2. How do you install a package globally?
Use the -g flag: npm install -g . Global packages are installed system-wide and can be used from any directory via command line. Example: npm install -g nodemon. Use with caution as global packages can cause version conflicts. List globals with npm list -g --depth=0.
Q3. What's the difference between dependencies and devDependencies?
dependencies are packages required for the application to run in production (like express, mongoose). devDependencies are only needed during development (like testing libraries, nodemon, build tools). Use --save-dev flag to add to devDependencies. This separation helps optimize production installs (npm install --production).
Q4. How do you install a specific version of a package?
Specify the version after @ symbol: npm install express@4.18.0. This installs exactly that version. You can also use version ranges: npm install express@^4.0.0 (compatible with 4.x.x). To install the latest version regardless of package.json, use npm install express@latest.
Q5. How do you uninstall a package?
Use npm uninstall (or npm remove). For local packages, it removes from node_modules and updates package.json. For global packages, add -g: npm uninstall -g . You can also use npm prune to remove extraneous packages not listed in package.json.
