@E18e/Is Npm

1.0.5Last update Aug 9, 2024
by@e18e

Replace is-npm Module Codemod

Introduction

This codemod replaces the is-npm npm module with a more efficient implementation using built-in Node.js environment variables. It eliminates the dependency on the library, reducing bundle size and improving performance by leveraging native features.

Before

import { isNpm, isYarn, isNpmOrYarn } from 'is-npm';
if (isNpm) {
console.log('Running in npm environment');
}
if (isYarn) {
console.log('Running in yarn environment');
}
if (isNpmOrYarn) {
console.log('Running in either npm or yarn environment');
}

After

if (
(process.env.npm_config_user_agent &&
process.env.npm_config_user_agent.startsWith('npm')) ||
(process.env.npm_package_json &&
process.env.npm_package_json.endsWith('package.json')) ||
(process.env.npm_config_user_agent &&
process.env.npm_config_user_agent.startsWith('yarn'))
) {
console.log('Running in npm or yarn environment');
} else if (process.env.npm_config_user_agent &&
process.env.npm_config_user_agent.startsWith('npm')) {
console.log('Running in npm environment');
} else if (process.env.npm_config_user_agent &&
process.env.npm_config_user_agent.startsWith('yarn')) {
console.log('Running in yarn environment');
}

Build custom codemods

Use AI-powered codemod studio and automate undifferentiated tasks for yourself, colleagues or the community

background illustrationGet Started Now