or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/npm-is-npm

Utility library for detecting whether JavaScript code is running within npm or yarn script execution context through environment variable checks

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/is-npm@6.0.x

To install, run

npx @tessl/cli install tessl/npm-is-npm@6.0.0

0

# is-npm

1

2

Check if your code is running as an [npm](https://docs.npmjs.com/misc/scripts) or [yarn](https://yarnpkg.com/lang/en/docs/cli/run/) script. This package provides three boolean constants that detect the package manager execution context by analyzing environment variables set by npm and yarn.

3

4

## Package Information

5

6

- **Package Name**: is-npm

7

- **Package Type**: npm

8

- **Language**: JavaScript (ES modules) with TypeScript definitions

9

- **Node.js**: ^12.20.0 || ^14.13.1 || >=16.0.0

10

- **Installation**: `npm install is-npm`

11

12

## Core Imports

13

14

```javascript

15

import { isNpm, isYarn, isNpmOrYarn } from "is-npm";

16

```

17

18

For TypeScript:

19

20

```typescript

21

import { isNpm, isYarn, isNpmOrYarn } from "is-npm";

22

```

23

24

## Basic Usage

25

26

```javascript

27

import { isNpmOrYarn, isNpm, isYarn } from "is-npm";

28

29

// Check if running under any package manager

30

if (isNpmOrYarn) {

31

console.log("Running as an npm or yarn script!");

32

}

33

34

// Check specifically for npm

35

if (isNpm) {

36

console.log("Running as an npm script!");

37

}

38

39

// Check specifically for yarn

40

if (isYarn) {

41

console.log("Running as a yarn script!");

42

}

43

44

// Conditional logic based on execution context

45

console.table({ isNpmOrYarn, isNpm, isYarn });

46

```

47

48

## Capabilities

49

50

### Package Manager Detection

51

52

Provides boolean constants to detect the current package manager execution context.

53

54

```javascript { .api }

55

/**

56

* Check if code is running as an npm script

57

* Detects both npm 6 (via user agent) and npm 7+ (via package.json path)

58

*/

59

export const isNpm: boolean;

60

61

/**

62

* Check if code is running as a yarn script

63

* Detects yarn by checking the user agent environment variable

64

*/

65

export const isYarn: boolean;

66

67

/**

68

* Check if code is running as either npm or yarn script

69

* Logical OR combination of isNpm and isYarn

70

*/

71

export const isNpmOrYarn: boolean;

72

```

73

74

## Detection Mechanism

75

76

The package uses environment variables set by npm and yarn during script execution:

77

78

- **npm detection**: Checks `process.env.npm_config_user_agent` (npm 6) and `process.env.npm_package_json` (npm 7+)

79

- **yarn detection**: Checks `process.env.npm_config_user_agent` starting with "yarn"

80

- **Combined detection**: Returns true if either npm or yarn is detected

81

82

## Usage Patterns

83

84

Common use cases for package manager detection:

85

86

```javascript

87

import { isNpmOrYarn } from "is-npm";

88

89

// Conditional behavior in build scripts

90

if (isNpmOrYarn) {

91

// Code runs only when executed via package manager

92

console.log("Running in development mode");

93

} else {

94

// Code runs when executed directly with node

95

console.log("Running in production mode");

96

}

97

```

98

99

```javascript

100

import { isNpm, isYarn } from "is-npm";

101

102

// Package manager specific logic

103

if (isNpm) {

104

console.log("Using npm-specific configuration");

105

} else if (isYarn) {

106

console.log("Using yarn-specific configuration");

107

} else {

108

console.log("Running outside package manager context");

109

}

110

```

111

112

## Environment Variables

113

114

The package relies on these environment variables automatically set by package managers:

115

116

- `npm_config_user_agent`: Contains package manager name and version (both npm and yarn)

117

- `npm_package_json`: Contains path to package.json (npm 7+ only)

118

119

These variables are automatically available when scripts are executed via `npm run` or `yarn run` commands.