简体   繁体   中英

Install npm package without dependencies

I am looking for best solution how to install npm package without it's dependencies described in it's package.json file.

The goal is to change dependencies versions before install package. I can do it manually for one package by downloading source, but if you have many nested dependencies it becomes a problem.

Here's a shell script that seems to get you the extracted files you need.

#!/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 it as npm_download.sh and run it with the name of the package you want:

./npm_download.sh pathval

Please check simialr quesition on stackexchange: https://unix.stackexchange.com/questions/168034/is-there-an-option-to-install-an-npm-package-without-dependencies

My solution was to rename package.json to package.bak before the install, then reverting rename afterwards:

RENAME package.json package.bak
npm install <package_name> --no-save
RENAME package.bak package.json

I supplemented the above script to allow the specification of multiple packages, avoid the temporary downloaded file and to install the packages straight to 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

I found this script useful for situations where dependencies are too strict with their dependency requirements, you can install deps you know work ok without adding them to your package.json until the upstream dependency is updated.

I adapted the script from mxcl to handle scoped packages (@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


Warning: mac has a weird version of sed, it should work but I can't guarantee.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM