I have written an articles explaining how to install npm packages programmatically. There’s a function called getNpmClient.
Detect tnpm/cnpm client
This comment below and the code show us how you could detect tnpm/cnpm client.
// detect tnpm/cnpm client
// all situations:
// - npminstall mode + native fs => generate _resolved field in package.json
// - npminstall mode + rapid fs => generate .package-lock.json in node_modules
// - npm mode + native fs => generate .package-lock.json in node_modules
// - npm mode + rapid fs => generate .package-lock.json in node_modules
// all conditions:
// - has _resolved field or .package-lock.json means tnpm/cnpm
// - _resolved field or .package-lock.json contains tnpm registry means tnpm
if (chokidarPkg._resolved) {
return tnpmRegistries.some((r) => chokidarPkg._resolved.includes(r))
? 'tnpm'
: 'cnpm';
} else if (existsSync(tcnpmLockPath)) {
const tcnpmLock = readFileSync(tcnpmLockPath, 'utf-8');
return tnpmRegistries.some((r) => tcnpmLock.includes(r)) ? 'tnpm' : 'cnpm';
}
Detect tnpm client
has _resolved field or .package-loc.json means tnpm/cnpm
This comment explains what this below if block is about
if (chokidarPkg._resolved) {
return tnpmRegistries.some((r) => chokidarPkg._resolved.includes(r))
? 'tnpm'
: 'cnpm';
}
Detect tnpm client
- _resolved field or .package-lock.json contains tnpm registry means tnpm
This comment above explains the else if block below
else if (existsSync(tcnpmLockPath)) {
const tcnpmLock = readFileSync(tcnpmLockPath, 'utf-8');
return tnpmRegistries.some((r) => tcnpmLock.includes(r)) ? 'tnpm' : 'cnpm';
}
This is the first time I am learning about tnpm or cnpm. I will write an article about these.
But what is tnpm or cnpm client?
tnpm Client
tnpm is an enterprise service for Alibaba and Ant Group, which is also based on cnpm, with additional enterprise-level customization.
cnpm Client
cnpm is an open-source implementation of npm, which supports mirror synchronization with the official npm registry and private package capabilities.
There is an in-depth of tnpm rapid mode — how tnpm managed to be 10 second faster than pnpm
This article talks in depth about
Why is npm soooo slow?
What are tnpm and cnpm?
Optimization results and much more.
About me:
Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.
I am open to work on interesting projects. Send me an email at ramu.narasinga@gmail.com
My Github — https://github.com/ramu-narasinga
My website — https://ramunarasinga.com
My Youtube channel — https://www.youtube.com/@thinkthroo
Learning platform — https://thinkthroo.com
Codebase Architecture — https://app.thinkthroo.com/architecture
Best practices — https://app.thinkthroo.com/best-practices
Production-grade projects — https://app.thinkthroo.com/production-grade-projects
Top comments (0)