monsoul-share is a Node 24 shared package for reusable backend capabilities across projects.
Current modules:
logger: lightweight file/stdout logger with request logging middleware.redis: lazy Redis client manager built on top ofioredis.
- Node 24
- npm
npm install monsoul-shareBefore publishing, confirm the package name is still available on npm.
const share = require('monsoul-share');
share.logger.configure({
appName: 'my-service',
level: 'debug',
dir: './logs',
toStdout: true,
toFile: true
});
const logger = share.logger('app');
const redis = share.createRedisService({
enabled: true,
url: process.env.REDIS_URL,
lazyConnect: true,
logger
});Basic usage:
const share = require('monsoul-share');
share.logger.configure({
appName: 'my-service',
level: 'debug',
dir: './logs',
toStdout: true,
toFile: true
});
const logger = share.logger('seminarService');
logger.info('service started');
logger.error('something failed: %s', 'demo');Express request logging:
const express = require('express');
const share = require('monsoul-share');
const app = express();
const logger = share.logger('http');
app.use(share.logger.express(logger));Supported log levels:
tracedebuginfowarnerrorfatal
Useful logger methods:
share.logger(scope)share.logger.configure(options)share.logger.express(loggerOrLevel)share.createLogger(scope)share.createRequestLogger(loggerOrLevel)
Logger defaults:
toFiledefaults totrue.- Default log directory is
<caller-project-root>/logs(auto-detected by nearestpackage.json). - You can still override via
share.logger.configure({ dir })orLOG_DIR.
Basic usage:
const share = require('monsoul-share');
const redis = share.createRedisService({
enabled: true,
url: process.env.REDIS_URL,
lazyConnect: true
});
async function main() {
await redis.connect();
await redis.getClient().set('healthcheck', 'ok');
const value = await redis.getClient().get('healthcheck');
console.log(value);
await redis.disconnect();
}
main().catch(console.error);Legacy-compatible standalone shape is also supported:
const redis = share.createRedisService({
enable: true,
host: process.env.REDIS_HOST || '127.0.0.1',
port: Number(process.env.REDIS_PORT || 6379),
password: process.env.REDIS_PASSWORD || null
});Cluster usage:
const share = require('monsoul-share');
const redis = share.createRedisService({
enabled: true,
clusterNodes: [
{ host: '127.0.0.1', port: 7000 },
{ host: '127.0.0.1', port: 7001 },
{ host: '127.0.0.1', port: 7002 }
],
lazyConnect: true
});Legacy-compatible cluster shape is also supported:
const redis = share.createRedisService({
enable: true,
isCluster: true,
servers: [
{
host: process.env.REDIS_CLUSTER_HOST || '127.0.0.1',
port: Number(process.env.REDIS_CLUSTER_PORT || 7000)
}
],
options: {
redisOptions: { password: process.env.REDIS_PASSWORD || null }
}
});Useful Redis methods:
share.createRedisService(options)redis.connect()redis.getClient()redis.getPublisher()redis.getSubscriber()redis.publish(channel, payload)redis.disconnect()redis.getStatus()
Note:
- Redis is disabled by default.
- Default standalone connection is
127.0.0.1:6379with no password. - No Redis connection is created until you call
connect(),getClient(),getPublisher(), orgetSubscriber().
Use Node 24 before running commands:
export NVM_DIR="$HOME/.nvm"
. "$NVM_DIR/nvm.sh"
nvm use 24Run tests:
npm testCheck the package contents before publishing:
npm pack --dry-run --cache /tmp/monsoul-share-npm-cacheBefore the first publish:
- Make sure
package.jsonmetadata is correct. - Confirm the npm name is available.
- Run
npm login. - Verify the logged-in account with
npm whoami. - Run
npm test. - Run
npm pack --dry-run --cache /tmp/monsoul-share-npm-cache. - Make sure the git working tree is clean before tagging.
Useful commands:
npm view monsoul-share version --registry=https://registry.npmjs.org
npm login
npm whoami
npm test
npm pack --dry-run --cache /tmp/monsoul-share-npm-cacheIf this is the first public release and the current version is already correct:
git status
npm test
npm pack --dry-run --cache /tmp/monsoul-share-npm-cache
git add .
git commit -m "chore: release v0.1.0"
git tag -a v0.1.0 -m "Release v0.1.0"
npm publish
git push origin main
git push origin v0.1.0If your default branch is not main, replace it with the actual branch name.
Recommended versioning:
patch: bug fixes only, for example0.1.0 -> 0.1.1minor: backward-compatible features, for example0.1.0 -> 0.2.0major: breaking changes, for example0.1.0 -> 1.0.0
Recommended release flow after the first publish:
- Finish code changes and commit them.
- Run
npm test. - Run
npm version patch,npm version minor, ornpm version major. - Run
npm publish. - Push the commit and tag to GitHub.
Example:
git status
npm test
npm version patch
npm publish
git push origin main --tagsIf you want to control the version manually without creating a git tag automatically:
npm version 0.2.0 --no-git-tag-version
git add package.json package-lock.json
git commit -m "chore: release v0.2.0"
git tag -a v0.2.0 -m "Release v0.2.0"
npm publish
git push origin main --tagsApache-2.0. See LICENSE.