The JS plugin for Nx contains executors and generators that provide the best experience for developing JavaScript and TypeScript projects.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
@nx/js provides several additional utility functions for package management, lock files, dependency handling, and development workflows.
Advanced package.json copying and management with watch mode support and dependency handling.
/**
* Copies and updates package.json files with dependency management and watch support
* @param options - Package JSON copy configuration
* @param context - Nx executor context
* @returns Promise resolving to success status with optional stop function for watch mode
*/
function copyPackageJson(
options: CopyPackageJsonOptions,
context: ExecutorContext
): Promise<CopyPackageJsonResult>;
interface CopyPackageJsonOptions extends Omit<UpdatePackageJsonOption, 'projectRoot'> {
watch?: boolean; // Enable watch mode for automatic updates
extraDependencies?: DependentBuildableProjectNode[]; // Additional dependencies to include
overrideDependencies?: DependentBuildableProjectNode[]; // Override dependency list
}
interface CopyPackageJsonResult {
success?: boolean; // Operation success status
stop?: () => void; // Stop function for watch mode (only when watch: true)
}Usage Example:
import { ExecutorContext } from "@nx/devkit";
import { copyPackageJson } from "@nx/js";
// Copy package.json with watch mode
const result = await copyPackageJson({
outputPath: 'dist/libs/my-lib',
generatePackageJson: true,
watch: true
}, context);
// Stop watching when done
if (result.stop) {
result.stop();
}Creates and manages package.json entry points for multi-format publishing.
/**
* Creates entry points configuration for package.json exports field
* @param main - Main entry file path
* @param outputPath - Output directory path
* @param projectRoot - Project root directory
* @param additionalEntryPoints - Additional entry points to include
* @param format - Output format configuration
* @returns Entry points configuration object
*/
function createEntryPoints(
main: string,
outputPath: string,
projectRoot: string,
additionalEntryPoints?: string[],
format?: SupportedFormat[]
): Record<string, any>;Usage Example:
import { createEntryPoints } from "@nx/js";
// Create entry points for dual CJS/ESM publishing
const entryPoints = createEntryPoints(
'src/index.ts',
'dist/libs/my-lib',
'libs/my-lib',
['src/feature.ts'],
['cjs', 'esm']
);Utilities for creating and managing package manager lock files.
/**
* Creates a lock file for the specified package manager
* @param packageManager - Package manager type
* @param workspaceRoot - Workspace root directory
* @param projectRoot - Project root directory
* @param packageJson - Package.json contents
* @returns Promise resolving to lock file creation result
*/
function createLockFile(
packageManager: 'npm' | 'yarn' | 'pnpm',
workspaceRoot: string,
projectRoot: string,
packageJson: any
): Promise<void>;
/**
* Gets the appropriate lock file name for the package manager
* @param packageManager - Package manager type
* @returns Lock file name
*/
function getLockFileName(packageManager: 'npm' | 'yarn' | 'pnpm'): string;Usage Example:
import { createLockFile, getLockFileName } from "@nx/js";
// Create lock file for npm
await createLockFile('npm', '/workspace', 'libs/my-lib', packageJson);
// Get lock file name
const lockFileName = getLockFileName('pnpm'); // Returns 'pnpm-lock.yaml'Utilities for managing local package registry scripts and configurations.
/**
* Adds local registry scripts to package.json for development workflows
* @param tree - Virtual file system tree
* @param options - Local registry configuration options
* @returns Updated package.json content
*/
function addLocalRegistryScripts(
tree: Tree,
options: LocalRegistryOptions
): void;
interface LocalRegistryOptions {
port?: number; // Registry port (default: 4873)
registryUrl?: string; // Custom registry URL
scopes?: string[]; // Package scopes to configure
}Usage Example:
import { Tree } from "@nx/devkit";
import { addLocalRegistryScripts } from "@nx/js";
// Add local registry scripts
addLocalRegistryScripts(tree, {
port: 4873,
scopes: ['@myorg']
});Manages tslib and other TypeScript runtime dependencies.
/**
* Adds tslib dependencies to the project based on TypeScript configuration
* @param tree - Virtual file system tree
* @param options - Dependency addition options
* @returns Generator callback for package installation
*/
function addTslibDependencies(
tree: Tree,
options: TslibDependencyOptions
): GeneratorCallback;
interface TslibDependencyOptions {
projectRoot: string; // Project root directory
skipPackageJson?: boolean; // Skip package.json updates
importHelpers?: boolean; // Enable TypeScript importHelpers
}Usage Example:
import { Tree } from "@nx/devkit";
import { addTslibDependencies } from "@nx/js";
// Add tslib dependencies
const installTask = addTslibDependencies(tree, {
projectRoot: 'libs/my-lib',
importHelpers: true
});
// Execute installation
await installTask();Utility for validating JavaScript/TypeScript variable names.
/**
* Validates if a string is a valid JavaScript/TypeScript variable name
* @param name - Variable name to validate
* @returns True if valid variable name, false otherwise
*/
function isValidVariable(name: string): boolean;Usage Example:
import { isValidVariable } from "@nx/js";
// Validate variable names
console.log(isValidVariable('myVariable')); // true
console.log(isValidVariable('my-variable')); // false
console.log(isValidVariable('123invalid')); // falseInstall with Tessl CLI
npx tessl i tessl/npm-nx--js