2

I have a Node module whose source-code is in a Git repository (GitHub). I can install the module into an NPM project from Git using NPM:

npm install --save [email protected]:user/example.git

The problem is that there are some build steps that need to be done after pulling the source-code. I don't want to include the results of the build in Git because they are artefacts of the build step, not true source-code.

Currently, I need to manually move to the directory (cd ./node_modules/example) and then run the build script (npm run build), but this is a hassle.

Since everything is in NPM, can this be automated somehow?

1 Answer 1

1

You could do it from postinstall hook like so from the package.json that includes the module:

"scripts": {
    "postinstall": "cd ./node_modules/example && npm run build"
}

Here's a good resource on npm postinstall:

http://krasimirtsonev.com/blog/article/Fun-playing-with-npm-dependencies-and-postinstall-script

5
  • 4
    This script will be called every time when a new package is installed, not sure that this is good.
    – alexmac
    Commented Aug 5, 2016 at 19:06
  • 1
    If you add this to the source package's package.json that gets pulled down as dependency, then it should not execute every time a new package is installed in the package where this package is being used. Makes sense?
    – Shaunak
    Commented Aug 5, 2016 at 19:10
  • unless you don't control the original package. In that case, just add a check in the build to ignore re-build.
    – Shaunak
    Commented Aug 5, 2016 at 19:13
  • Yes it makes sense, but only if you have access to this repo.
    – alexmac
    Commented Aug 5, 2016 at 19:14
  • 1
    Exactly. So if you don't have access to the source repo like I said, it would mean you are doing some custom building on that package for your own application. So it would make sense to move the building logic in your build script (grunt/gulp/whatever.. ) and just check whether it needs building. Post install would still be your best bet IMO
    – Shaunak
    Commented Aug 5, 2016 at 19:17

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.