Download source from npm without installing it

后端 未结 6 1512
既然无缘
既然无缘 2020-12-13 01:34

How can I download the source code of a package from npm without actually installing it (i.e. without using npm install thepackage)?

相关标签:
6条回答
  • 2020-12-13 01:50

    On linux I usually download the tarball of a package like this:

    wget `npm v [package-name] dist.tarball`
    

    Notice the backticks ``, on stackoverflow I cannot see them clearly.

    "v" is just another alias for view:

    https://docs.npmjs.com/cli/view

    0 讨论(0)
  • 2020-12-13 01:51

    npm pack XXX is the quickest to type and it'll download an archive.

    Alternatively:

    npm v XXX dist.tarball | xargs curl | tar -xz
    

    this command will also:

    • Download the package with progress bar
    • Extracts into a folder called package
    0 讨论(0)
  • 2020-12-13 01:54

    If you haven't installed npm, with the current public API, you can also access the information about a package in the npm registry from the URL https://registry.npmjs.org/<package-name>/.

    Then you can navigate the JSON at versions > (version number) > dist > tarball to get the URL of the code archive and download it.

    0 讨论(0)
  • 2020-12-13 01:56

    A simpler way to do this is npm pack <package_name>. This will retrieve the tarball from the registry, place it in your npm cache, and put a copy in the current working directory. See https://docs.npmjs.com/cli/pack

    0 讨论(0)
  • 2020-12-13 01:57

    Based on Gustavo Rodrigues's answer, fixes "package" directory in .tgz, adds latest minor version discovery.

    #!/bin/bash
    
    if [[ $# -eq 0 ]] ; then
        echo "Usage: $0 jquery bootstrap@3 tinymce@4.5"
        exit 64 ## EX_USAGE
    fi
    
    set -e ## So nothing gets deleted if download fails
    
    for pkg_name in "$@"
    do
    
        ## Get latest version, also works with plain name
        url=$( npm v $pkg_name dist.tarball | tail -n 1 | cut -d \' -f 2 )
        tmp_dir=$( mktemp -d -p . "${pkg_name}__XXXXXXXXX" )
    
        ## Unpacks to directory named after package@version
        curl $url | tar -xzf - --strip 1 --directory $tmp_dir
        rm -rf $pkg_name
        mv $tmp_dir $pkg_name
    done
    
    0 讨论(0)
  • 2020-12-13 02:06

    You can use npm view [package name] dist.tarball which will return the URL of the compressed package file.

    Here's an example using wget to download the tarball:

    wget $(npm view lodash dist.tarball)
    
    0 讨论(0)
提交回复
热议问题