Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install NPM package under alias or different name

How can I npm install a package into a different directory?

like image 970
Vincent Avatar asked May 14 '19 16:05

Vincent


People also ask

Can you change the name of an NPM package?

There's no way to rename a package, you have to deprecate and publish a new package.

What is npm alias?

Npm aliases are when you install a package, but with an aliased name. So this means you can install the package, but instead of using the standard npm name, you can use your own! To do this, you just run the following when doing your install: npm install <ALIAS_NAME>@npm:<REAL_PACKAGE>@<PACKAGE_VERSION>

How do I install a specific version of npm?

Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.

What is NPM alias install?

npm install <alias>@npm:<name>: Install a package under a custom alias. Allows multiple versions of a same-name package side-by-side, more convenient import names for packages with otherwise long ones and using git forks replacements or forked npm packages as replacements.

What is npm and how to use it?

The node package manager (npm) installs the packages required in a JavaScript project ​and provides a useful interface on which these packages can work. The npm install command allows the user to install a package. There are two types of installation: The following command is used to install packages using npm.

How does NPM install the latest version of a package?

If there is a package.json file, npm installs the latest version that satisfies the semver rule declared in package.json. Scoped public packages can be downloaded and installed by anyone, as long as the scope name is referenced during installation:

What is the difference between NPM install--link and--no bin-links?

npm install (in package directory, no arguments): Install the dependencies in the local node_modules folder. ... The --link argument will cause npm to link global installs into the local space in some cases. The --no-bin-links argument will prevent npm from creating symlinks for any binaries the package might contain.


2 Answers

Say you want to install Case package, you can have a specific version under an alias:

npm i case-1.5.3@npm:[email protected] 

or just give it a different name

npm i kool@npm:case 

If you want to edit package.json directly:

"dependencies": {   "case-1.5.3": "npm:case@^1.5.3",   "kool": "npm:case@^1.6.1" } 

require():

let Case = require( 'case-1.5.3' ); let Kool = require( 'kool' ); 

Yarn used to have this functionality for a long time, and npm finally got it since v6.9.0, Mar 2019.

If you want to update your npm:

sudo npm i -g npm@latest 
like image 132
Vincent Avatar answered Oct 05 '22 18:10

Vincent


with PNPM
if want to use two different versions of a package in your project. It is possible with following commands

pnpm add <any-alias-name>@npm:package-name  for example   pnpm add new-lodash@npm:lodash@2 pnpm add old-lodash@npm:lodash@1 

Now we can use both lodash in our project

const newLodash = require('new-lodash'); const oldLodash = require('old-lodash'); 

Note that it worked only for require and not for ESM import statement i.e.

import oldLodash from 'old-lodash' // will throw error 
like image 44
Akshay Vijay Jain Avatar answered Oct 05 '22 18:10

Akshay Vijay Jain