or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

angular-cli-adapter.mdconfiguration.mdindex.mdlogging.mdpackage-manager.mdproject-graph.mdtree-api.mdworkspace-management.mdworkspace-root.md

package-manager.mddocs/

0

# Package Manager

1

2

Cross-platform package manager utilities for detecting and working with npm, yarn, and pnpm. Provides automatic detection, command generation, and registry operations across different package management systems.

3

4

## Capabilities

5

6

### Package Manager Detection

7

8

Automatically detect which package manager is being used in a project based on lock files.

9

10

```typescript { .api }

11

/**

12

* Detects package manager based on lock files in the directory

13

* @param dir - Directory to check for lock files (defaults to current working directory)

14

* @returns The detected package manager type

15

*/

16

export function detectPackageManager(dir?: string): PackageManager;

17

18

/** Supported package managers */

19

export type PackageManager = 'yarn' | 'pnpm' | 'npm';

20

```

21

22

**Usage Examples:**

23

24

```typescript

25

import { detectPackageManager } from "@nrwl/tao/shared/package-manager";

26

27

// Detect in current directory

28

const pm = detectPackageManager();

29

console.log(`Using ${pm} package manager`);

30

31

// Detect in specific directory

32

const projectPm = detectPackageManager("/path/to/project");

33

console.log(`Project uses ${projectPm}`);

34

```

35

36

### Command Generation

37

38

Get package manager-specific commands for common operations.

39

40

```typescript { .api }

41

/**

42

* Returns command structure for the specified package manager

43

* @param packageManager - Package manager to get commands for (auto-detected if not provided)

44

* @returns Object containing commands for the package manager

45

*/

46

export function getPackageManagerCommand(packageManager?: PackageManager): PackageManagerCommands;

47

48

/** Command structure interface for package managers */

49

export interface PackageManagerCommands {

50

/** Install command (e.g., 'npm install', 'yarn install') */

51

install: string;

52

/** Add dependency command */

53

add: string;

54

/** Add dev dependency command */

55

addDev: string;

56

/** Remove dependency command */

57

rm: string;

58

/** Execute command */

59

exec: string;

60

/** Download and execute command */

61

dlx: string;

62

/** List dependencies command */

63

list: string;

64

}

65

```

66

67

**Usage Examples:**

68

69

```typescript

70

import { getPackageManagerCommand, detectPackageManager } from "@nrwl/tao/shared/package-manager";

71

72

const pm = detectPackageManager();

73

const commands = getPackageManagerCommand(pm);

74

75

console.log(`Install command: ${commands.install}`);

76

console.log(`Add dev dependency: ${commands.addDev} jest`);

77

console.log(`Execute script: ${commands.exec} build`);

78

79

// Example outputs for different package managers:

80

// npm: { install: "npm install", addDev: "npm install --save-dev", exec: "npx" }

81

// yarn: { install: "yarn install", addDev: "yarn add --dev", exec: "yarn" }

82

// pnpm: { install: "pnpm install", addDev: "pnpm add --save-dev", exec: "pnpm exec" }

83

```

84

85

### Version Detection

86

87

Get the version of the detected or specified package manager.

88

89

```typescript { .api }

90

/**

91

* Gets the version of the specified package manager

92

* @param packageManager - Package manager to check (auto-detected if not provided)

93

* @returns Version string of the package manager

94

*/

95

export function getPackageManagerVersion(packageManager?: PackageManager): string;

96

```

97

98

**Usage Examples:**

99

100

```typescript

101

import { getPackageManagerVersion, detectPackageManager } from "@nrwl/tao/shared/package-manager";

102

103

const pm = detectPackageManager();

104

const version = getPackageManagerVersion(pm);

105

console.log(`${pm} version: ${version}`);

106

107

// Check specific package manager version

108

const yarnVersion = getPackageManagerVersion('yarn');

109

console.log(`Yarn version: ${yarnVersion}`);

110

```

111

112

### Configuration Management

113

114

Find and work with package manager configuration files.

115

116

```typescript { .api }

117

/**

118

* Finds .npmrc files in the project hierarchy

119

* @param directory - Directory to start searching from (defaults to current working directory)

120

* @returns Path to the .npmrc file or null if not found

121

*/

122

export function checkForNPMRC(directory?: string): string | null;

123

```

124

125

**Usage Examples:**

126

127

```typescript

128

import { checkForNPMRC } from "@nrwl/tao/shared/package-manager";

129

130

const npmrcPath = checkForNPMRC();

131

if (npmrcPath) {

132

console.log(`Found .npmrc at: ${npmrcPath}`);

133

} else {

134

console.log("No .npmrc file found");

135

}

136

137

// Check in specific directory

138

const projectNpmrc = checkForNPMRC("/path/to/project");

139

```

140

141

### Temporary Operations

142

143

Create temporary directories for isolated package operations.

144

145

```typescript { .api }

146

/**

147

* Creates temporary directory for package operations

148

* @returns Object with temporary directory path and cleanup function

149

*/

150

export function createTempNpmDirectory(): { dir: string; cleanup: () => Promise<void> };

151

```

152

153

**Usage Examples:**

154

155

```typescript

156

import { createTempNpmDirectory } from "@nrwl/tao/shared/package-manager";

157

158

async function isolatedPackageOperation() {

159

const { dir, cleanup } = createTempNpmDirectory();

160

161

try {

162

console.log(`Working in temporary directory: ${dir}`);

163

// Perform package operations in isolated environment

164

} finally {

165

await cleanup(); // Always clean up

166

}

167

}

168

```

169

170

### Registry Operations

171

172

Resolve package versions and download packages from registries.

173

174

```typescript { .api }

175

/**

176

* Resolves package versions via npm registry

177

* @param packageName - Name of the package to resolve

178

* @param version - Version or version range to resolve

179

* @returns Promise resolving to the exact version

180

*/

181

export function resolvePackageVersionUsingRegistry(packageName: string, version: string): Promise<string>;

182

183

/**

184

* Resolves versions by installing in temp directory

185

* @param packageName - Name of the package to resolve

186

* @param version - Version or version range to resolve

187

* @returns Promise resolving to the exact version

188

*/

189

export function resolvePackageVersionUsingInstallation(packageName: string, version: string): Promise<string>;

190

191

/**

192

* Executes npm/pnpm view commands

193

* @param pkg - Package name

194

* @param version - Package version

195

* @param args - Additional arguments for the view command

196

* @returns Promise resolving to command output

197

*/

198

export function packageRegistryView(pkg: string, version: string, args: string): Promise<string>;

199

200

/**

201

* Downloads package tarballs

202

* @param cwd - Working directory

203

* @param pkg - Package name

204

* @param version - Package version

205

* @returns Promise resolving to tarball path

206

*/

207

export function packageRegistryPack(cwd: string, pkg: string, version: string): Promise<{ tarballPath: string }>;

208

```

209

210

**Usage Examples:**

211

212

```typescript

213

import {

214

resolvePackageVersionUsingRegistry,

215

packageRegistryView,

216

packageRegistryPack

217

} from "@nrwl/tao/shared/package-manager";

218

219

// Resolve exact version from registry

220

const exactVersion = await resolvePackageVersionUsingRegistry("@nx/workspace", "^15.0.0");

221

console.log(`Resolved version: ${exactVersion}`);

222

223

// Get package information

224

const packageInfo = await packageRegistryView("@nx/workspace", "latest", "description");

225

console.log(`Package description: ${packageInfo}`);

226

227

// Download package tarball

228

const { tarballPath } = await packageRegistryPack("/tmp", "@nx/workspace", "15.9.7");

229

console.log(`Downloaded tarball to: ${tarballPath}`);

230

```

231

232

## Integration with Workspace Operations

233

234

The package manager utilities integrate with other @nrwl/tao APIs:

235

236

```typescript

237

import { detectPackageManager, getPackageManagerCommand } from "@nrwl/tao/shared/package-manager";

238

import { Workspaces } from "@nrwl/tao/shared/workspace";

239

import { logger } from "@nrwl/tao/shared/logger";

240

241

async function setupWorkspaceDependencies(root: string) {

242

const workspaces = new Workspaces(root);

243

const nxConfig = workspaces.readNxJson();

244

245

const pm = detectPackageManager(root);

246

const commands = getPackageManagerCommand(pm);

247

248

logger.info(`Using ${pm} package manager`);

249

logger.info(`Install command: ${commands.install}`);

250

251

// Configure CLI to use detected package manager

252

if (nxConfig.cli) {

253

nxConfig.cli.packageManager = pm;

254

}

255

}

256

```