Home  Nodejs   Local vs gl ...

Local vs. Global Installation of nodejs package - npx use case

Suppose you have installed tsc package locally in your project (npm i tsc). Note here we have not used -g flag (global installation). Then you can run tsc via "npx tsc" command. Using npx tsc instead of tsc has a few benefits related to how Node.js and npm manage dependencies and executables:

1. Local vs. Global Installation

2. Using npx

npx is a tool that comes with npm (version 5.2.0 and higher) and is designed to execute binaries from the node_modules/.bin directory. It allows you to run commands from locally installed packages without needing to install them globally.

Benefits of Using npx tsc:

  1. Consistency: Ensures you are using the project-specific version of TypeScript, avoiding potential version conflicts between projects.
  2. No Global Installation Required: You don't need to install TypeScript globally, which keeps your global environment clean.
  3. Ease of Use: You don't need to remember to add ./node_modules/.bin to your PATH. npx handles this automatically.

Practical Example

  1. Local Installation of TypeScript:

    npm install typescript --save-dev
    
  2. Using npx tsc:

    npx tsc
    

Why Not Just Use tsc?

If you run tsc without npx in a project with only a local installation of TypeScript, you'll get an error like command not found: tsc. This is because the tsc command is not available globally unless you have installed TypeScript globally.

Published on: Jun 25, 2024, 11:39 PM  
 

Comments

Add your comment