Create a new Lerna repo or upgrade an existing repo to the current version of Lerna
npx @tessl/cli install tessl/npm-lerna--init@6.6.0Lerna Init creates new Lerna repositories or upgrades existing repositories to the current version of Lerna. It handles the initialization of monorepo project structures by creating essential configuration files, setting up directory structures, and configuring workspace dependencies.
npm install @lerna/initimport initFactory from "@lerna/init";
import { InitCommand } from "@lerna/init";
import command from "@lerna/init/command";CommonJS:
const initFactory = require("@lerna/init");
const { InitCommand } = require("@lerna/init");
const command = require("@lerna/init/command");import initFactory from "@lerna/init";
// Create InitCommand instance using factory
const initCommand = initFactory(process.argv);
// Execute initialization
await initCommand.execute();Using InitCommand directly:
import { InitCommand } from "@lerna/init";
// Create command instance
const initCommand = new InitCommand(process.argv);
await initCommand.execute();Using as command module:
import command from "@lerna/init/command";
// Command configuration for CLI
const initModule = {
command: "init",
describe: "Create a new Lerna repo or upgrade an existing repo to the current version of Lerna",
builder: command.builder,
handler: command.handler
};Lerna Init is built around several key components:
Creates new InitCommand instances for repository initialization.
/**
* Factory function that creates new InitCommand instances
* @param argv - Command line arguments from Node.js process
* @returns InitCommand instance ready for execution
*/
function factory(argv: NodeJS.Process["argv"]): InitCommand;Main command class that handles Lerna repository initialization logic.
/**
* Command class for initializing Lerna repositories
* Extends the base Command class from @lerna/core
*/
class InitCommand extends Command<InitCommandOptions> {
/** Whether to use exact version matching for dependencies */
exact?: boolean;
/** Version of Lerna to install in the repository */
lernaVersion: string;
/** Whether Git repository is required (always false for init) */
get requiresGit(): boolean;
/**
* Initialize the command, optionally creating Git repository
* @returns Promise that resolves when initialization is complete
*/
override initialize(): Promise<void>;
/**
* Execute the initialization process
* @returns Promise that resolves when all initialization steps complete
*/
override execute(): Promise<void>;
/**
* Skip validation phase (overridden for init command)
*/
runValidations(): void;
/**
* Skip preparation phase (overridden for init command)
*/
runPreparations(): void;
/**
* Ensure .gitignore file exists with node_modules exclusion
* @returns Promise that resolves when .gitignore is created or exists
*/
ensureGitIgnore(): Promise<void>;
/**
* Create or update lerna.json and package.json configuration files
* @returns Promise that resolves when configuration is complete
*/
ensureConfig(): Promise<void | Package>;
/**
* Create packages directory structure for monorepo
* @returns Promise that resolves when directories are created
*/
ensurePackagesDir(): Promise<void[]>;
}Yargs command module configuration for CLI integration.
/**
* Yargs command module for the init command
*/
interface CommandModule {
/** Command name used in CLI */
command: "init";
/** Command description for help text */
describe: "Create a new Lerna repo or upgrade an existing repo to the current version of Lerna";
/** Command options builder configuration */
builder: {
exact: {
describe: "Specify lerna dependency version in package.json without a caret (^)";
type: "boolean";
};
independent: {
describe: "Version packages independently";
alias: "i";
type: "boolean";
};
};
/**
* Command handler that executes the init command
* @param argv - Parsed command line arguments
* @returns InitCommand instance created by factory
*/
handler(argv: any): InitCommand;
}Types and interfaces from the Lerna core system.
/**
* Base command class from @lerna/core that InitCommand extends
*/
abstract class Command<T extends CommandConfigOptions> {
constructor(argv: NodeJS.Process["argv"]);
abstract initialize(): Promise<void>;
abstract execute(): Promise<void>;
}
/**
* Package representation from @lerna/core
*/
interface Package {
name: string;
version: string;
location: string;
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
serialize(): Promise<void>;
}
/**
* Base command configuration options from @lerna/core
*/
interface CommandConfigOptions {
_?: string[];
concurrency?: number;
sort?: boolean;
maxBuffer?: number;
stream?: boolean;
loglevel?: string;
verbose?: boolean;
progress?: boolean;
npmClient?: string;
}
/**
* Options interface for InitCommand configuration
*/
interface InitCommandOptions extends CommandConfigOptions {
/** Use exact version matching for lerna dependency (no caret prefix) */
exact: boolean;
/** Version of Lerna to install as dependency */
lernaVersion: string;
}The init command supports the following options:
Specify lerna dependency version in package.json without a caret (^) prefix.
lerna init --exactThis creates exact version matching and saves the setting in lerna.json:
{
"command": {
"init": {
"exact": true
}
}
}Version packages independently rather than using fixed versioning.
lerna init --independent
# or
lerna init -iThis sets the version field in lerna.json to "independent".
Main Lerna configuration file created with:
Root package.json created/updated with:
Git ignore file created with node_modules exclusion if not present.
Directory structure created for monorepo packages.