Installing an npm Package Excluding Its Dependencies

Question: What is the best way to install an npm package without installing its dependencies from the package.json file?
Solution 1: Save the package as a local file and install it using the package name.
Solution 2: Check out this similar question on StackExchange: https://unix.stackexchange.com/questions/168034/is-there-an-option-to-install-an-npm-package-without-dependencies. My solution involves renaming the package.json file to package.bak before the installation, and then reverting the rename afterward.
Solution 3: I have modified the script above to allow for the installation of multiple packages, avoid downloading temporary files, and install the packages directly. I found this script useful in situations where dependencies have strict requirements, and you can install known working dependencies without adding them to your package.json until the upstream dependency is updated.


Question:

I am searching for the optimal approach to exclude the dependencies mentioned in the package.json file while
install npm
packaging.

The objective is to modify the versions of dependencies pre-
install package
. While altering one package’s version manually can be done by downloading its source, it can pose a challenge when dealing with numerous nested dependencies.


Solution 1:

I have a shell script that appears to provide the required
extracted files
value.

#!/bin/bash
package="$1"
version=$(npm show ${package} version)
archive="${package}-${version}.tgz"
curl --silent --remote-name 
  "https://registry.npmjs.org/${package}/-/${archive}"
mkdir "${package}"
tar xzf "${archive}" --strip-components 1 -C "${package}"
rm "${archive}"

Save the code as

npm_download.sh

and execute it by specifying the desired package name.


./npm_download.sh pathval


Solution 2:


Kindly refer to a related query on stackexchange at https://unix.stackexchange.com/questions/168034/for-installing-msdt-a1-without-dependencies-is-there-any-option-available.

To avoid any issues during installation, I opted to rename the “package.json” file to “package.bak” prior to the process, and then changed it back to its original name once the installation was complete.

RENAME package.json package.bak
npm install  --no-save
RENAME package.bak package.json


Solution 3:


To enhance the previous script, I made adjustments that permit the selection of numerous packages, eliminate the need for a downloaded file, and enable direct installation of the packages into

node_modules

.

#!/bin/sh
# filename suggestion: `npm-i-no-deps`
while [ $# -gt 0 ]
do
  package="$1"
  version=$(npm show ${package} version)
  mkdir -p "node_modules/${package}"
  echo "Installing ${package}-${version}"
  curl --silent "https://registry.npmjs.org/${package}/-/${package}-${version}.tgz" | tar xz --strip-components 1 -C "node_modules/${package}"
  shift
done

This script comes in handy when dependencies have strict requirements. Instead of adding known working dependencies to

package.json

, you can install them separately and wait for the upstream dependency to be updated.


Solution 4:

I modified mxcl’s script to support packages with scopes, such as (@user/package).

#!/bin/sh
# filename suggestion: `npm-i-no-deps`
while [ $# -gt 0 ]
do
  package="$1"
  version=$(npm show ${package} version)
  mkdir -p "node_modules/${package}"
  echo "Installing ${package}-${version}"
  packagename=$(echo $package | sed 's/@.*///g')
  curl --silent "https://registry.npmjs.org/${package}/-/${packagename}-${version}.tgz" | tar xz --strip-components 1 -C "node_modules/${package}" 
  shift
done

Please be advised that the version of sed on mac is unusual, and while it is expected to function, I cannot provide assurance.

Frequently Asked Questions

Posted in Npm