NPM package with dependencies for global installation only? - npm-install

If an NPM package is designed both as a global command and as a dependency for other applications / packages, may I specify its dependencies for different installation cases?
#zh-CN 假设一个 NPM 包既可作为全局命令,也可以作为其他应用程序或包的依赖项。那么,在 package.json 中是否可以为不同的安装模式指定不同的依赖项?
E.g., package mypkg depends on package foo when it runs as a global command. However, foo is unnecessary when mypkg is installed and used as a dependency for other applications / packages. How to prevent foo installed when -g | --global option is absent?
#zh-CN 例如,mypkg 作为全局命令运行时,需要依赖 foo;但如果它仅作为其他应用程序或包的依赖项,则并不依赖 foo。怎样可以在非全局安装模式下,阻止安装 foo?
In another word, I mean
#zh-CN 换句话说,我的意思是
# Please DO NOT install **foo**.
npm install mypkg
# Please DO install **foo**.
npm install --global mypkg

The simple answer is, install any package globally only if you require it. Whenever a package or project requires another package to run as a dependency, then install it as a dependency. This way, whenever anyone installs the package and tries running it through an npm script, the package will run fine, because you have specified the correct version and package that has to be installed in order for the script to run without any errors.

Related

Skip a Specific npm Package on Build Definition on ADO

There are some packages that I'd like to skip on build definition. I was wondering if there's any way to skip any specific npm packages on the build definition on ADO?
For example, if I want to ignore the npm package of #microsoft/applicationinsights-common, can I do something like below in the npm
install --ignore #microsoft/applicationinsights-common
There is no possiblity to ignore one specific npm package using npm install.
If you want to skip such npm packages, that are only needed for development, than in package.json move the development specific dependencies to devDependencies and use npm install --production command to install the packages that are defined in dependencies.

npm 5 install folder without using symlink

Before publishing my node library, I could use the advice the npm documentation wrote about:
To test a local install, go into some other folder, and then do:
cd ../some-other-folder
npm install ../my-package
Prior to version 5 of npm, I had no problem as it produce what I expected, ie a folder with the output of what I will publish.
However, using npm 5, it now creates a symlink to my local project as described in the npm documentation:
npm install :
Install the package in the directory as a symlink in the current
project. Its dependencies will be installed before it's linked. If
sits inside the root of your project, its dependencies may be
hoisted to the toplevel node_modules as they would for other types of
dependencies.
How can I use the "old" way to install local project? Or is there a new way to check if my library is correct?
Thank you.
Use npm pack + npm install (as suggested by install-local package)
npm pack <path-to-local-package>
npm install <package-version.tgz>
This will effectively copy your local package to node_modules.
Note that this will package only production relevant files (those listed in the files section of your package.json). So, you can install it in a test app under the package own directory. Something like this:
my-package
package.json
test
test-app
package.json
node_modules
my-package
Assuming that test dir is not included in the files in my-package/package.json.
This works the same way with npm 5 and older versions.
I wrote npm-install-offline which allows you to install npm packages from a local repository or folder. By default it copies the folder on install but you can also choose to symlink.
https://www.npmjs.com/package/npm-install-offline
npx npm-install-offline ../some-package
Or
npx npm-install-offline my-npm-package --repo ./my-offline-npm
It also will install the package dependencies which npm does not do with local packages.

Installing npm globally

Is it possible to install npm globally and is this a good idea?
I installed npm with the npm install command and was able to run npm start. Then after publishing my project to github I wanted to make sure it would run if someone cloned it, so I cloned it to a different directory on my machine. I then had to run npm install again to install the dependencies. Is it necessary to do this for each project you build locally or is it better and possible to install it globally on your machine?
Thanks
Command line for install npm globally--
npm install -g <package>
For more read from here.
In general, the rule of thumb is:
If you’re installing something that you want to use in your program,
using require('whatever'), then install it locally, at the root of
your project.
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.
Details you can read here.
To install a module from npm globally, you'll simply need to use the --global flag when running the install command to have the module install globally, rather than locally (to the current directory).
you can use command:
npm install <module> --globalor npm install <module> -g
note: <module> is the name of the module you want to install globally

What is the difference between brew, yarn, and npm?

I was using the react-native package which I installed globally with npm. Now it says at the first line after executing the init command. The following:
Installing react-native from npm...
Consider installing yarn to make this faster: https://yarnpkg.com
So I was checking that website and it looked interesting to me, but I don't exactly know what it would be. At first, I thought that I would need brew to install yarn, so I could yarn to install npm. But now I think that yarn is a replacement of npm. Is that a correct statement?
Why would I like to have so many package managers?
I understand that it's useful for software like Atom or Visual Studio Code to have their own package manager. But for development, I do not see the reason why someone would like to use four different package managers (brew for 'primary software', yarn for npm packages, npm for backend modules and bower for front-end libraries). How can this package manager forest be untangled?
I am not familiar with brew, but I suppose you mean the Homebrew software package management system for macOS.
Then the purpose of each system is:
brew: installation of software, i.e. ready to consume applications like wget.
npm: installation of packages (libraries), i.e. pieces of functionality to help you build your own applications.
yarn: also installation of packages.
Yarn has some advantages over npm, the main two are the speed and the predictability. Yarn reuses the npm's package.json file and doesn't change its structure. Therefore you can run yarn install instead of npm install and theoretically everything will work automatically.
P.S. I agree, https://yarnpkg.com doesn't have enough background on why the hell we need another package management system, but there is a great article which fills that gap.
yarn vs npm
yarn and npm are both manage module installations and dependencies. Yarn was built to address some of the shortcomings of npm.
The biggest advantages of yarn over npm are
Installing packages with yarn is parallelized and so package installation is faster.
package.json can be very loose in terms of version numbers. yarn.lock (similar to npm shirkwrap) locks this down so that two machines with the same package.json always install the exact same packages.
yarn allows you to check why some packages are installed (understand the dependency tree)
Ref: https://www.sitepoint.com/yarn-vs-npm/
Yarn is a JavaScript package manager built by Facebook, Google, Exponent, and Tilde. It is created to remove or overcome the features that lack in npm. In comparison with npm it has
Enhanced Security
Offline mode
Parallel Installation - Therefore, faster installation
Another major difference was the yarn.lock file, but after npm ^5.x.x they provide the package-lock.json file too.
And the commands of yarn works like npm:
# Starting a new project
npm init === yarn init
# Installing all the dependencies of the project
npm install === yarn or yarn install
# Adding a dependency
npm install [package] === yarn add [package] # The package is saved to your package.json immediately.
npm install [package]#[version] === yarn add [package]#[version]
npm install [package]#[tag] === yarn add [package]#[tag]
# Add a dev dependency
npm install [package] --save-dev === yarn add [package] --dev
# Upgrading a dependency
npm update [package] === yarn upgrade [package]
npm update [package]#[version] === yarn upgrade [package]#[version]
npm update [package]#[tag] === yarn upgrade [package]#[tag]
# Removing a dependency
npm uninstall [package] === yarn remove [package]
# View registry information
npm view [package] === yarn info [package]
# List installed packages
npm list === yarn list
npm list --depth === yarn list --depth=0
# Install packages globally
npm install -g [package] === yarn global addb [package]
# Run a defined package script
npm run [script] === yarn run [script]
Refferences
https://www.sitepoint.com/yarn-vs-npm/
https://scotch.io/#brian_kimo/npm-vs-yarn
and the official announcement
https://code.facebook.com/posts/1840075619545360
Yarn is, like NPM, a package manager for Node.JS.
Yarn is built by Facebook.
It's faster and has more features than NPM.
Their main selling points are:
Security With yarn.lock file (similar to NPM's npm-shrinkwrap.json)
all dependencies are locked on the exact version. So, you don't have that “But it works on my machine” struggles anymore. Everyone has the
same versions locked in yarn.lock file
Speed Yarn uses (fast) proxies and (offline) caching to deliver your
modules faster. It also has a LICENSE checker, which checks the
license of all your dependency modules.

When installing locally (npm install .), can I have npm use my global packages?

When I run npm install . it takes a while to build packages that contain c code like expresso (which depends on node-jscoverage). I realized that I can copy expresso from my global package directory (~/Developer/lib/node_modules/expresso) to ./node_modules/expresso in my current directory before running npm install . and it won't bother compiling it. Is there a way to tell npm to try to install packages from my global npm directory before fetching and building them?
I guess this command might help: npm link
Check out this: npm to install packages from local position rather than from web?