What are global packages?


Editorial team Published on

Global packages are software tools or libraries installed in a single, centralized location on your computer rather than inside a specific project folder.

How They Work

  • System-Wide Access: When you install a package globally (using a flag like -g in npm), your computer places it in a central system directory.
  • Command Line Tools: They usually provide executable command-line tools (CLI) that you can run from your terminal in any folder or project.
  • Not in Code: You generally do not import global packages directly into your application's source code using statements like require() or import.

Global vs. Local Packages

  • Local Packages: Installed inside a specific project's node_modules folder. They are meant to be used directly by that project's code (like express or react) and are tracked in that project's configuration file.
  • Global Packages: Saved on your machine for your use as a developer. They are independent of any single project.

Common Examples

  • Nodemon: A tool that restarts your Node.js server automatically when code files change.
  • create-react-app / Angular CLI: Command-line utilities used to scaffold new web projects.
  • TypeScript / Prettier: Tools used globally or locally to compile or format code.

Best Practices

  • Use Sparingly: Installing too many global packages can cause version conflicts if different projects require different versions of the same tool.
  • Alternatives: For many command-line tools, modern package managers let you run them directly without a permanent global install using tools like npx.