Local versus global packages for Angular development - npm

When developing with Angular, one can
npm install
to install packages locally, or
npm install -g
to install them globally. I am wondering what are the implications of each practice. And what happens if a particular package is installed both ways, perhaps with different versions? Which one will my Angular app use?

From there https://www.quora.com/When-do-I-install-a-package-locally-or-globally-in-NPM
In general, the rule of thumb is:
1. If you’re installing something that you want to use in your program, using
import { 'whatever'}, then install it locally, at the root of your project.
2. If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

Related

npx from command line does not find imports?

I'm trying to run a simple hello.ts script from command line. This works if the script has no dependencies:
npx ts-node hello.ts
But as soon as I start adding some dependencies...
import _ from 'lodash';
console.log('hello');
It fails:
Cannot find module 'lodash' or its corresponding type declarations.
It keeps failing even if I install the dependencies globally. So how do I tell npx (or ts-node for that matter) to consider globally installed dependencies?
Update
Using Node 16.9.1 (upgraded via Version Lens). The error seems to have disappeared after uninstalling/reinstalling the imported libraries a few times.
If you're using npm >=1.0, you can use npm link to create a local link to a package already installed globally. (Caveat: The OS must support symlinks.)
IE: npm install -g lodash && npm link lodash
However, this doesn't come without its problems.
npm link is a development tool. It's awesome for managing packages on your local development box. But deploying with npm link is basically asking for problems, since it makes it super easy to update things without realizing it.
As an alternative, you can install the packages locally as well as globally.
For additional information, see:
https://nodejs.org/en/blog/npm/npm-1-0-link/
https://nodejs.org/en/blog/npm/npm-1-0-global-vs-local-installation/
Are you using the n package by any chance? I used n to change from a newer version of node (16.2.0) to an older version of node (12.13.0), ran npm i and npx failed with a different error.
Using n to change back to 16.2.0 seems to have resolved the issue so I'm thinking perhaps it was an issue with package-lock.json or such

Should we install a Package Locally or Globally with npm in React Native?

I am developing a React Native App, in which I have installed some Packages Locally and some Global and I can see Local and Global Packages via listing them with these Commands:
For Local Packages:
npm list
For Global Packages:
npm list -g
Right now, I am a bit Confused that whether I should install Packages Locally or Globally? What are the pros and cons of installing locally and globally? Also, which one is recommend?
The main difference is where the package is stored on the disk and re-usage.
Local packages are installed in your project directory (/node-modules) depending on your setup.
Global packages are stored somewhere in your user directory. (Varies per system)
Usually, modules used for building/generating files are usually installed globally, The rest are installed locally.
this might be a good read to learn the differences: https://nodejs.dev/learn/npm-global-or-local-packages

Is there any difference between installing global packages with Yarn or NPM?

Does it matter whether you install a global package with yarn global add PACKAGE vs npm install -g PACKAGE ?
Is there any difference at all, like where files are installed?
If yes, what is it?
So yes, you are right it is different. For npm it is something like below
/Users/tarunlalwani/.nvm/versions/node/v9.2.0/lib if you are using nvm
You can get this path using
$ npm config get prefix
/Users/tarunlalwani/.nvm/versions/node/v9.2.0
Where does npm install packages?
While yarn uses other paths
Windows: %LOCALAPPDATA%/Yarn/config/global
OSX and Linux non-root: ~/.config/yarn/global
Linux if logged in as root: /usr/local/share/.config/yarn/global
How to display yarn globally installed packages?
See this thread as well
https://github.com/yarnpkg/yarn/issues/2049
This is the document about Yarn global
yarn global is a prefix used for a number of commands like add, bin,
list and remove. They behave identically to their normal versions
except that they use a global directory to store packages. The global
command makes executables available to use on your operating system
and this is the document about npm install global mode
In global mode (ie, with -g or --global appended to the command), it
installs the current package context (ie, the current working
directory) as a global package.
I think there is no difference between them. Install a package as a global useful for developer tooling that is not part of any individual project but instead is used for local commands

Gulp install on Windows (Npm issue)

I'm trying to install Gulp for my Angular UI project and I'm a bit surprised by the fact that it won't install due to it's dependencies. So, node installed fine, but npm.js refuses to install due to file path too long error. My folder structure is 75 characters long, of the 260 available characters, that leaves 185 characters for npm to use. Am I missing something here, or do the npm authors expect me to fire up a linux box for my UI? (A deal breaker)
Update:
What is the best way to install gulp as a dependency for my Angular UI project? (My goal is to ultimately have gulp become part of my TFS CI)
Ignoring your path length problem for now (you may want to split your question), as far as how to get gulp installed, you just need to include it in your package.json file as a dependency.
You can do that by running npm install --save gulp
However, that does need npm installed first.
The easiest way to do this is to download and install Node from:
https://nodejs.org/en/download/
This will install Node and npm globally, which should avoid your path length problem and in my experience is the standard approach (I've not worked with TFS, but all other CI pipelines I've worked with support Node via a container image or build option/step).
If you dont want to manually install Node, you can use something like Chocolatey to install it automatically (you can install Chocolatey from https://chocolatey.org/ and then you can run choco install nodejs from your command line).

Why does coffeescript need to be installed globally?

I have a jenkins build that is failing with the following error:
+ npm install
npm WARN prefer global coffee-script#1.12.4 should be installed with -g
Curious as to why coffee-script, or any package for that matter, needs to be installed globally?
Because coffeescript is a command line tool which can transpile coffeescript into javascript, or run as as an interactive shell similar to node.
from the NPMJS docs:
There are two ways to install npm packages: locally or globally. You choose which kind of installation to use based on how you want to use the package.
If you want to use it as a command line tool, something like the grunt CLI, then you want to install it globally. On the other hand, if you want to depend on the package from your own module using something like Node's require, then you want to install locally.
It would technically be possible to install these CLI packages locally, but then you would have to run them using a relative path such as(untested):
./node_modules/coffeescript/bin/coffeescript