How can I npm install
a package into a different directory?
There's no way to rename a package, you have to deprecate and publish a new package.
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>
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.
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.
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.
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:
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With