npm install and build of forked github repo

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 prepare script, but I'm not sure if that's possible; I've never tried it. Edit: Cameron Tacklind has written an excellent answer detailing how to do this: https://stackoverflow.com/a/57829251/7127751


TL;DR use a prepare script

and don't forget package.json#files or .npmignore

Code published to npmjs.com is often not what's in the repository for the package. It is common to "compile" JavaScript source files into versions meant for general consumption in libraries. That's what's usually published to npmjs.com.

It is so common that it's a feature of npm to automatically run a "build" step before publishing (npm publish). This was originally called prepublish. It seems that Npm thought it would be handy to also run the prepublish script on an npm install since that was the standard way to initialize a development environment.

This ended up leading to some major confusion in the community. There are very long issues on Github about this.

In the end, in an effort to not change old behavior, they decided to add two more automatic scripts: prepublishOnly and prepare.

prepublishOnly does what you expect. It does not run on npm install. Many package maintainers just blindly switched to this.

But there was also this problem that people wanted to not depend on npmjs.com to distribute versions of packages. Git repositories were the natural choice. However it's common practice to not commit "compiled" files to git. That's what prepare was added to handle...


prepare is the correct way

If you have a repository with source files but a "build" step is necessary to use it,
prepare does exactly what you want in all cases (as of npm 4).

prepare: Run both BEFORE the package is packed and published, on local npm install without any arguments, and when installing git dependencies.

You can even put your build dependencies into devDependencies and they will be installed before prepare is executed.

Here is an example of a package of mine that uses this method.


Problems with .gitignore

There is one issue with this option that gets many people. When preparing a dependency, Npm and Yarn will keep only the files that are listed in the files section of package.json.

One might see that files defaults to all files being included and think they're done. What is easily missed is that .npmignore mostly overrides the files directive and, if .npmignore does not exist, .gitignore is used instead.

So, if you have your built files listed in .gitignore, like a sane person, and don't do anything else, prepare will seem broken.

If you fix files to only include the built files or add an empty .npmignore, you're all set.

My recommendation

Set files (or, by inversion, .npmignore) such that the only files actually published are those needed by users of the published package. Imho, there is no need to include uncompiled sources in published packages.


Original answer: https://stackoverflow.com/a/57503862/4612476


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.


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.


Just use the command npm install git+https://[email protected]/myRepo/angular-translate.git. Thanks.