7

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.

Cyprian Gepfert
  • 373
  • 4
  • 15
  • 2
    This is a unique problem for which I doubt there's a way or a tool out there to do it. You'll have to code it yourself, most likely. [Post-install scripts](https://docs.npmjs.com/misc/scripts) may aid you. – laggingreflex Feb 07 '15 at 14:23
  • 1
    Possible duplicate of [NPM install single package without rest of dependencies](https://stackoverflow.com/questions/49732031/npm-install-single-package-without-rest-of-dependencies) – Josem Apr 09 '18 at 12:41

4 Answers4

9

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

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • if you have your own npm registry; replace `https://registry.npmjs.org` with a variable, e.g. `${regurl}`, and pass it as an argument, e.g. `regurl="$2"` – Eliran Malka Nov 13 '16 at 08:53
2

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
Vitaliy Ulantikov
  • 10,157
  • 3
  • 61
  • 54
0

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.

mxcl
  • 26,392
  • 12
  • 99
  • 98
0

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.

naugtur
  • 16,827
  • 5
  • 70
  • 113