npm install and build of forked github repo

2019-01-10 08:19发布

This is not a totally new question, but I've been looking around for a good while now and I'm having trouble finding a solution.

I'm using a module for my angular app called angular-translate. However, I've had to make a few small modifications to the source code to get everything working the way I'd like, and now I want to persist those changes on npm install. A colleague suggested that I fork the repo of the source code and point to my forked repo as a dependency, which I've tried in these ways, e.g.

npm install https://github.com/myRepo/angular-translate
npm install https://github.com/myRepo/angular-translate/archive/master.tar.gz

The first gives me a directory like this with no build. Just a package.json, .npmignore, and some markdown files

-angular-translate
   .npmignore 
   .nvmrc
    CHANGELOG.md 
    package.json
    etc

The second npm install gives me the full repo, but again I don't get a build like when I use the command npm install angular-translate. I've seen some dicussion of running the prepublish script, but I'm not sure how to do this when installing all the modules. I've also tried publishing the fork as my own module to the npm registry, but again I get no build, and I'm not sure that's the right thing to do...

I apologise for my ignorance on the topic. I don't have a huge amount of experience with npm. Would love to get some feedback on this issue. It seems like it could be a common enough issue when modifications need to be made to a package's source code? Maybe there's a better solution? Thanks in advance for your help.

4条回答
Viruses.
2楼-- · 2019-01-10 09:01

Try npm install <ghusername>/<repoName>, where <ghUsername> is your GitHub username (without the @) and <repoName> is the name of the repository. That should correctly install it. You will most likely want to use the --save or --save-dev flag with the install command to save dependency in your package.json.

If that isn't working correctly, check the contents of your .npmignore file.

Don't panic if the install command takes a long time; installing from a git repository is slower than installing from the npm registry.


Edit:

Your problem is that in your case, dist/ is not committed to the repo (since it is in the .gitignore). That is where the actual code lives. dist/ is built from the files in src/ before the package is published to the npm registry, but dist/ is never committed to the repo.

It's ugly, but in this case you will have to remove dist/ from the .gitignore and then run:

npm run build
git add .
git commit
git push

(Ensure that you have run npm install first)

You then should be able to install from github.

There might be another way to do this using a postinstall script, but I'm not sure if that's possible; I've never tried it.

查看更多
三岁会撩人
3楼-- · 2019-01-10 09:03

To piggyback off of @RyanZim's excellent answer, postinstall is definitely a valid option for this.

Either do one of the following:

  1. Update the package.json in your forked repo to add a postinstall element to scripts. In here, run whatever you need to get the compiled output (Preferred).
  2. Update your package.json, and add a postinstall that updates the necessary directory in node_modules.

If you've forked another persons repository, then it might be worth raising an issue to illustrate the issue that installing their package through GitHub does not work as it does not provide the necessary means to build the script. From there, they can either accept a PR to resolve this with a postinstall, or they can reject it and you can do #2.

查看更多
Melony?
4楼-- · 2019-01-10 09:04

Update for those using npm 5:

As of npm@5, prepublish scripts are deprecated.

Use prepare for build steps and prepublishOnly for upload-only.

I found adding a "prepare": "npm run build" to scripts fixed all my problems.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-10 09:22

Just use the command npm install git+https://git@github.com/myRepo/angular-translate.git. Thanks.

查看更多
登录 后发表回答