How to use npm config for dependent modules?

Viewed 136

My Node application uses a module as a dependency. Such module uses a third party SDK as a dependency:

Main package.json

{
  "name": "Main",
  "dependencies": {
    "library": "^0.0.0"
  }
}

Library package.json

{
  "name": "library",
  "dependencies": {
    "third-party-sdk": "^0.0.0"
  }
}

That third party SDK accepts a NPM CONFIG parameter sdk_type to select whether to use (download in its postinstall) the production or development version. I need to always force this value to development

So if I type npm config set sdk_type development, or create a .npmrc in the main project, thigs work as expected.

However, I´d rather configure this from the library project, so when the library is included in any project, it takes care of configuring the 3rd party sdk appropriately.

Seems the npm config can only be set from the root package.json?

Things I have tried:

1- include a npmrc in the library file. (failed)

2- create a "preinstall script" in library´s package.json:

{
  "name": "library",
  "dependencies": {
    "sdk_module": "^1.15.0"
  },
  "scripts": {
    "preinstall": "npm config set sdk_type development"
  }
}

This doesn´t really work either, as it looks like the configuration is not updated until I run NPM again. So the ongoing install will fail as well, even though a second would succeed.

3- set an ENVVAR that is also available for that purpose in the "preinstall script":

{
  "name": "library",
  "dependencies": {
    "sdk_module": "^1.15.0"
  },
  "scripts": {
    "preinstall": "export SDK_TYPE=development"
  }
}

In this case I think the variable is not in scope for the third party SDK postinstall.

Is this at all possible? I feel I am fighting with NPM, there has to be a simpler way to do this.

0 Answers
Related