Could you explain the distinction between npm install and sudo npm install?

I recently switched to using linux. To install typescript, I ran the following command: npm i typescript Although there were no errors during the installation process, when I checked the version by typing tsc --version, I encountered the error message

-bash: /usr/bin/tsc: No such file or directory
.

In an attempt to rectify this, I decided to reinstall typescript with elevated permissions using the command sudo npm i -g typescript. This time, the installation was successful and tsc --version provided the expected compiler version number.

My queries are:

  • What distinguishes between npm i typescript and sudo npm i -g typescript?
  • Was anything actually installed with the first command? If so, where was it located? And why did tsc --version fail initially?
  • How does running a command with sudo differ from running it without sudo? When is it advisable to use sudo and when should it be avoided?

Appreciate your insights!

Answer №1

When you use the -g flag with npm, it installs a module globally instead of in your current project. However, this practice is not recommended as it can lead to issues. It's better to utilize npx instead.

After running the initial command, did anything actually get installed? If so, where does it go? And why did "tsc --version" fail?

The package will be stored in the node_modules directory of your current project - a location not included in your $PATH environment variable. Therefore, it's advisable to use npx tsc --version.

sudo is used to run commands as root rather than a regular user. It may be necessary when installing global modules using a system-wide Node.js installation, if the ordinary user lacks write permissions for the system-wide module collection managed by tools like nvm.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transferring data types to a component and then sending it to a factory

I have been grappling with creating a factory method using Angular 2 and TypeScript. However, my attempts have hit a roadblock as the TSC compiler keeps throwing an unexpected error: error TS1005: ',' expected. The issue arises when I try to pa ...

An easy method to define argument types for every function type in TypeScript

How can I assign argument types to each function type in TypeScript? Each function has a parameter associated with it. [Example] type F1 = (arg: number) => void; type F2 = (arg: string) => void; const caller = (f: F1 | F2) => (n: number | strin ...

Exploring the power of Prosemirror with NextJS through Tiptap v2

Greetings everyone, I am a newcomer to Stack Overflow and I am reaching out for assistance regarding an issue that has arisen. The problem at hand pertains to the development of the Minimum Viable Product (MVP) for my startup which specializes in creating ...

What is the reason for using an outdated version of lodash in the code?

After successfully installing node on my Android phone using Termux, I decided to upgrade lodash globally to version 4. $ npm -v 5.6.0 $ node -v v8.9.2 $ npm i -g lodash + <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="19757 ...

Exploring Angular 2's nested navigation using the latest router technology

Is there a way to implement nested navigation in Angular? I had this functionality with the previous router setup. { path: '/admin/...', component: AdminLayoutComponent } It seems that since rc1 of angular2, this feature is no longer supported. ...

Utilize npm workspaces to refresh the package.json files nested within package-lock.json

I am utilizing npm workspaces to manage a monorepo. One thing I've noticed is that the top-level package-lock.json contains a cached version of each workspace's package.json in its "package" field. How can I update these cached versions without a ...

Ensuring consistency between TypeScript .d.ts and .js files

When working with these definitions: https://github.com/borisyankov/DefinitelyTyped If I am using angularJS 1.3.14, how can I be certain that there is a correct definition for that specific version of Angular? How can I ensure that the DefinitelyTyped *. ...

Ways to utilize a field from an interface as a type of index

interface Mapping { "alpha": (a: string) => void "beta": (b: number) => void } interface In<T extends keyof Mapping> { readonly type: T, method: Mapping[T] } const inHandlers: In<"alpha"> = { type ...

React-query v5 - Updating or fetching outdated query

I am currently using the Tanstack/react-query v5.12.2 library and I am facing an issue with invalidating or refetching an inactive query using the useQueryClient() function. It seems that something has changed in version 5 of the library. I have tried sett ...

Assessing the validity of a boolean condition as either true or false while iterating through a for loop

I am facing an issue that involves converting a boolean value to true or false if a string contains the word "unlimited". Additionally, I am adding a subscription to a set of values and need to use *NgIf to control page rendering based on this boolean. &l ...

Use the npm command key to globally install a package

My attempt to globally install gulp on OS X has led to a perplexing discovery. Initially, I used the command: npm install -g glup This resulted in an error message: npm ERR! Darwin 14.4.0 npm ERR! argv "node" "/usr/local/bin/npm" "install" "-g" "glup" n ...

Troubleshooting: Authentication guard not functioning properly in Angular 2 due to HTTP request

As I work on implementing a guard for certain routes in my application, I face an issue. To grant access to the route, my intention is to send an HTTP request to my express backend API and check if the user's session exists. I have explored various e ...

Checkbox selections persist when navigating between pages

I am currently working with Angular 9 and I have a list of checkboxes that need to default to true when displaying certain data. If one of these checkboxes is unchecked, it should trigger the display of specific information. The issue I am facing is that o ...

What is the best way to prevent a directory from being included in the Webpack bundle?

Issue: Despite configuring my Webpack settings in webpack.config.js to exclude files from the ./src/Portfolio directory, all files are being bundled by Webpack. Code Snippet: Webpack.config.js const path = require('path'); module.exports = { ...

Leveraging and utilizing TypeScript npm packages

My goal is to create shared code in TypeScript, package it as an npm package, and easily install it. I attempted to create an external library like this: export class Lib { constructor(){ } getData(){ console.log('getting data from l ...

How should I structure the .yarnrc file for optimal performance?

"@fortawesome:registry" "https://npm.fontawesome.com/" "//npm.fontawesome.com/:_authToken" "XXXXXXX-my-token" I am currently working on setting up the .yarnrc configuration for my project, but I'm uncertain about the correct file format. Does the abo ...

The use of the .reset() function in typescript to clear form data may lead to unexpected

I've been trying to use document.getelementbyID().reset(); to reset form values, but I keep running into an error in TypeScript. Property 'reset' does not exist on type 'HTMLElement'. Here's how I implemented it: const resetB ...

Show Timing on the Y-Axis - Bubble Graph

Recently, I stumbled upon the Bubble Chart feature in ng2-charts. I am trying to display data based on time on the Y-axis and values on the X-axis. My dataset consists of x:[10,35,60], y:["7.00 AM"], with r having the same value as x. However, the sample d ...

Discover properties of a TypeScript class with an existing object

I am currently working on a project where I need to extract all the properties of a class from an object that is created as an instance of this class. My goal is to create a versatile admin page that can be used for any entity that is associated with it. ...

How can we access a value within a deeply nested JSON object in Node.js when the key values in between are not

In the nested configuration object provided below, I am seeking to retrieve the value associated with key1, which in this case is "value1". It's important to note that while key1 remains static, the values for randomGeneratedNumber and randomGenerated ...