yarnpkg/yarn · error · MessageError
lockfileExists
Error message
lockfileExists
What it means
`yarn import` is designed as a one-time migration from npm to yarn; `init()` at `import.js:372` refuses to run if a `yarn.lock` already exists in `config.cwd`. It throws `lockfileExists` ('Lockfile already exists, not importing.') to avoid overwriting an existing yarn lockfile.
Source
Thrown at src/cli/commands/import.js:372
this.resolver.dependencyTree = new LogicalDependencyTree(packageJson, packageLock);
} catch (e) {
throw new MessageError(this.reporter.lang('importSourceFilesCorrupted'));
}
}
async getExternalLockfileContents(): Promise<{packageJson: ?string, packageLock: ?string}> {
try {
const [packageJson, packageLock] = await Promise.all([
fs.readFile(path.join(this.config.cwd, NODE_PACKAGE_JSON)),
fs.readFile(path.join(this.config.cwd, NPM_LOCK_FILENAME)),
]);
return {packageJson, packageLock};
} catch (e) {
return {packageJson: null, packageLock: null};
}
}
async init(): Promise<Array<string>> {
if (await fs.exists(path.join(this.config.cwd, LOCKFILE_FILENAME))) {
throw new MessageError(this.reporter.lang('lockfileExists'));
}
const {packageJson, packageLock} = await this.getExternalLockfileContents();
const importSource =
packageJson && packageLock && semver.satisfies(nodeVersion, '>=5.0.0') ? 'package-lock.json' : 'node_modules';
if (importSource === 'package-lock.json') {
this.reporter.info(this.reporter.lang('importPackageLock'));
this.createLogicalDependencyTree(packageJson, packageLock);
}
if (importSource === 'node_modules') {
this.reporter.info(this.reporter.lang('importNodeModules'));
await verifyTreeCheck(this.config, this.reporter, {}, []);
}
const {requests, patterns, manifest} = await this.fetchRequestFromCwd();
if (manifest.name && this.resolver instanceof ImportPackageResolver) {
this.resolver.rootName = manifest.name;
}
await this.resolver.init(requests, {isFlat: this.flags.flat, isFrozen: this.flags.frozenLockfile});
const manifests: Array<Manifest> = await fetcher.fetch(this.resolver.getManifests(), this.config);View on GitHub (pinned to c2dda503f3)
Solutions
- If migration is intentional, delete the existing `yarn.lock` and rerun `yarn import`.
- Otherwise treat this as an expected guard — your project already uses yarn; just run `yarn install`.
- Verify you are in the correct project root before importing.
Example fix
// before $ yarn import → Lockfile already exists, not importing. // after (re-import) $ rm yarn.lock && yarn import
Defensive patterns
Strategy: validation
Validate before calling
import fs from 'fs';
function assertNoYarnLock(cwd: string): void {
if (fs.existsSync(`${cwd}/yarn.lock`)) {
throw new Error('yarn.lock already exists; remove it before `yarn import` or skip import');
}
} Try / catch
try {
await yarnImport();
} catch (e) {
if (/Lockfile already exists, not importing/.test(e.message)) {
// expected guard — decide: delete yarn.lock to re-import, or just yarn install
} else throw e;
} Prevention
- Run `yarn import` exactly once, at npm→yarn migration time.
- If you see this error unexpectedly, you are likely in the wrong directory or already migrated.
When it happens
Trigger: `fs.exists(path.join(config.cwd, LOCKFILE_FILENAME))` (i.e. `yarn.lock` present) returns true at the top of `Import.init()`.
Common situations: Running `yarn import` after `yarn install` already created yarn.lock, a leftover yarn.lock from a previous attempt, or running import in the wrong directory.
Related errors
- workspacesAddRootCheck
- importResolveFailed
- importSourceFilesCorrupted
- frozenLockfileError
- noRequiredLockfile
AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13).
Data as JSON: /api/errors/2ade7e1440f6238e.
Report an issue: GitHub.