Forem

Ramu Narasinga
Ramu Narasinga

Posted on

You are familiar with npm/pnpm but have you heard of tnpm or cnpm?

I have written an articles explaining how to install npm packages programmatically. There’s a function called getNpmClient.

Image description

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';
  }
Enter fullscreen mode Exit fullscreen mode

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';
}
Enter fullscreen mode Exit fullscreen mode

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');
Enter fullscreen mode Exit fullscreen mode
  return tnpmRegistries.some((r) => tcnpmLock.includes(r)) ? 'tnpm' : 'cnpm';
}
Enter fullscreen mode Exit fullscreen mode

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

Image description

This article talks in depth about

  1. Why is npm soooo slow?

  2. What are tnpm and cnpm?

  3. 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

References:

  1. https://dev.to/atian25/in-depth-of-tnpm-rapid-mode-how-could-we-fast-10s-than-pnpm-3bpp

  2. https://github.com/cnpm/cnpm

  3. https://github.com/umijs/umi/blob/9c3194d0617276fbecd09d19a8cff606fcaac82d/packages/utils/src/npmClient.ts#L18

Top comments (0)