Reputation: 36199
Let's say I have a client, awesome-service
that composes of different types of services, http-service
, log-service
, etc...
I want to have the option of either including each individual service (and a specific version), or just require all of the awesome-services
, so in effect I want something like:
const awesomeService = require('@awesome-service');
// Now awesomeService has
// awesomeService.httpService;
// awesomeService.logService;
// etc
// or individually
const httpService = require('@awesome-service/http-service');
Is this possible? What would the package.json and the GitHub organization look like? Maybe this is package.json?
"dependencies": {
"awesome-service": "@awesome-service"
// OR if individually importing them
"http-service": "@awesome-service/http-service#1.0.0"
}
How can this be accomplished, or rather can this be accomplished?
Upvotes: 2
Views: 2095
Reputation: 19597
Is this possible?
Yes, it's possible.
What would the package.json and the GitHub organization look like?
The package should have the following structure:
- awesome-service
- index.js // main module
- package.json // package.json of the main package
- http-service
- index.js // implementation of `http` service
- package.json // package.json of `http` package
- log-service
- index.js // implementation of `log` service
- package.json // package.json of `log` package
As you see there are three package.json
files. The root is used for main package, others for each service.
In each package.json, set main
field to index.js
and set a correct name for each package:
{
"name": "awesome-service",
"main": "index.js",
...
}
{
"name": "awesome-service@http-service",
"main": "index.js",
...
}
{
"name": "awesome-service@log-service",
"main": "index.js",
...
}
In index.js
of the root package export the object with the fields - required services (I don't specify index.js in requires, because this module will be loaded by default):
module.exports = {
httpService: require('./http-service'),
logService: require('./log-service')
};
To use these three package separately, you should add all of them in npm, or use github with a proper url:
"dependencies": {
"awesome-service": "awesome-service"
"http-service": "awesome-service@http-service#1.0.0",
"log-service": "git+https://github.com/yourAccount/awesome-service/log-service.git"
}
Upvotes: 4