{"record":{"id":"2ade7e1440f6238e","repo":"yarnpkg/yarn","slug":"lockfileexists","errorCode":null,"errorMessage":"lockfileExists","messagePattern":"lockfileExists","errorType":"exception","errorClass":"MessageError","httpStatus":null,"severity":"error","filePath":"src/cli/commands/import.js","lineNumber":372,"sourceCode":"      this.resolver.dependencyTree = new LogicalDependencyTree(packageJson, packageLock);\n    } catch (e) {\n      throw new MessageError(this.reporter.lang('importSourceFilesCorrupted'));\n    }\n  }\n  async getExternalLockfileContents(): Promise<{packageJson: ?string, packageLock: ?string}> {\n    try {\n      const [packageJson, packageLock] = await Promise.all([\n        fs.readFile(path.join(this.config.cwd, NODE_PACKAGE_JSON)),\n        fs.readFile(path.join(this.config.cwd, NPM_LOCK_FILENAME)),\n      ]);\n      return {packageJson, packageLock};\n    } catch (e) {\n      return {packageJson: null, packageLock: null};\n    }\n  }\n  async init(): Promise<Array<string>> {\n    if (await fs.exists(path.join(this.config.cwd, LOCKFILE_FILENAME))) {\n      throw new MessageError(this.reporter.lang('lockfileExists'));\n    }\n    const {packageJson, packageLock} = await this.getExternalLockfileContents();\n    const importSource =\n      packageJson && packageLock && semver.satisfies(nodeVersion, '>=5.0.0') ? 'package-lock.json' : 'node_modules';\n    if (importSource === 'package-lock.json') {\n      this.reporter.info(this.reporter.lang('importPackageLock'));\n      this.createLogicalDependencyTree(packageJson, packageLock);\n    }\n    if (importSource === 'node_modules') {\n      this.reporter.info(this.reporter.lang('importNodeModules'));\n      await verifyTreeCheck(this.config, this.reporter, {}, []);\n    }\n    const {requests, patterns, manifest} = await this.fetchRequestFromCwd();\n    if (manifest.name && this.resolver instanceof ImportPackageResolver) {\n      this.resolver.rootName = manifest.name;\n    }\n    await this.resolver.init(requests, {isFlat: this.flags.flat, isFrozen: this.flags.frozenLockfile});\n    const manifests: Array<Manifest> = await fetcher.fetch(this.resolver.getManifests(), this.config);","sourceCodeStart":354,"sourceCodeEnd":390,"githubUrl":"https://github.com/yarnpkg/yarn/blob/c2dda503f3759b5be5f0e24ecd9cf5c97a540147/src/cli/commands/import.js#L354-L390","documentation":"`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.","triggerScenarios":"`fs.exists(path.join(config.cwd, LOCKFILE_FILENAME))` (i.e. `yarn.lock` present) returns true at the top of `Import.init()`.","commonSituations":"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.","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."],"exampleFix":"// before\n$ yarn import\n→ Lockfile already exists, not importing.\n\n// after (re-import)\n$ rm yarn.lock && yarn import","handlingStrategy":"validation","validationCode":"import fs from 'fs';\n\nfunction assertNoYarnLock(cwd: string): void {\n  if (fs.existsSync(`${cwd}/yarn.lock`)) {\n    throw new Error('yarn.lock already exists; remove it before `yarn import` or skip import');\n  }\n}","typeGuard":null,"tryCatchPattern":"try {\n  await yarnImport();\n} catch (e) {\n  if (/Lockfile already exists, not importing/.test(e.message)) {\n    // expected guard — decide: delete yarn.lock to re-import, or just yarn install\n  } else throw e;\n}","preventionTips":["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."],"tags":["yarn-import","lockfile","guard"],"backgroundTag":null,"analyzedSha":"c2dda503f3759b5be5f0e24ecd9cf5c97a540147","analyzedAt":"2026-08-13T04:17:06.305Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}