How to install an npm package from GitHub directly?

后端 未结 15 2062
失恋的感觉
失恋的感觉 2020-11-22 06:48

Trying to install modules from github results in:

ENOENT error on package.json.

Easily reproduced using express:

相关标签:
15条回答
  • 2020-11-22 07:38

    The general form of the syntax is

    <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]
    

    which means for your case it will be

    npm install git+ssh://git@github.com/visionmedia/express.git
    

    From npmjs docs:

    npm install :

    Installs the package from the hosted git provider, cloning it with git. For a full git remote url, only that URL will be attempted.

    <protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish>
    

    | #semver:] is one of git, git+ssh, git+http, git+https, or git+file.

    If # is provided, it will be used to clone exactly that commit. If the commit-ish has the format #semver:, can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither # or

    semver: is specified, then master is used.

    If the repository makes use of submodules, those submodules will be cloned as well.

    If the package being installed contains a prepare script, its dependencies and devDependencies will be installed, and the prepare script will be run, before the package is packaged and installed.

    The following git environment variables are recognized by npm and will be added to the environment when running git:

    • GIT_ASKPASS
    • GIT_EXEC_PATH
    • GIT_PROXY_COMMAND
    • GIT_SSH
    • GIT_SSH_COMMAND
    • GIT_SSL_CAINFO GIT_SSL_NO_VERIFY

    See the git man page for details.

    Examples:

    npm install git+ssh://git@github.com:npm/npm.git#v1.0.27
    npm install git+ssh://git@github.com:npm/npm#semver:^5.0
    npm install git+https://isaacs@github.com/npm/npm.git
    npm install git://github.com/npm/npm.git#v1.0.27
    GIT_SSH_COMMAND='ssh -i ~/.ssh/custom_ident' npm install git+ssh://git@github.com:npm/npm.git npm install
    
    0 讨论(0)
  • 2020-11-22 07:39

    There's also npm install https://github.com/{USER}/{REPO}/tarball/{BRANCH} to use a different branch.

    0 讨论(0)
  • 2020-11-22 07:40

    You can directly install an github repo by npm install command, like this: npm install https://github.com/futurechallenger/npm_git_install.git --save

    NOTE: In the repo which will be installed by npm command:

    1. maybe you have to have a dist folder in you repo, according to @Dan Dascalescu's comment.
    2. You definitely have to have a package.json in you repo! which I forget add.
    0 讨论(0)
提交回复
热议问题