Modifying a Project's package.json

I’m trying to add a postbuild script to the project that installed my module. It works when manually added, but I’d prefer it happen automatically after my module has been installed.

From what I understand, I need to add a postinstall entry in my module’s package.json pointing to a file like "postinstall": "node src/postinstall.js". But from there I’m at a loss how to modify the top level project’s package.json. Any help would be appreciated.

Thanks!

package.json is a json file, so you can write code to read the file and convert the json into a javascript object using plain node (there are lots of packages for helping with json), or use json command-line utilities to manipulate it.

I would caution again modifying the top-level package.json as part of the installation of a dependency if you intend for this package to be used by other people. I do not expect or want packages to be mucking with my package.json when I install them. You could add a command to offer this in an explicit way say:

$ npx your-module setup-wizard
This will add a postinstall script to your package, and [some other stuff]
Do you want to continue? (Y/n)

I managed to figure it out, but I like your suggestion of using a setup wizard.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.