{"record":{"id":"22329d98d22954bf","repo":"YMFE/yapi","slug":"init-lock-init-lock","errorCode":null,"errorMessage":"init.lock文件已存在，请确认您是否已安装。如果需要重新安装，请删掉init.lock文件","messagePattern":"init\\.lock文件已存在，请确认您是否已安装。如果需要重新安装，请删掉init\\.lock文件","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/install.js","lineNumber":15,"sourceCode":"const fs = require('fs-extra');\nconst yapi = require('./yapi.js');\nconst commons = require('./utils/commons');\nconst dbModule = require('./utils/db.js');\nconst userModel = require('./models/user.js');\nconst mongoose = require('mongoose');\n\nyapi.commons = commons;\nyapi.connect = dbModule.connect();\n\nfunction install() {\n  let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));\n\n  if (exist) {\n    throw new Error(\n      'init.lock文件已存在，请确认您是否已安装。如果需要重新安装，请删掉init.lock文件'\n    );\n  }\n\n  setupSql();\n}\n\nfunction setupSql() {\n  let userInst = yapi.getInst(userModel);\n  let passsalt = yapi.commons.randStr();\n  let result = userInst.save({\n    username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),\n    email: yapi.WEBCONFIG.adminAccount,\n    password: yapi.commons.generatePassword('ymfe.org', passsalt),\n    passsalt: passsalt,\n    role: 'admin',\n    add_time: yapi.commons.time(),\n    up_time: yapi.commons.time()","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/YMFE/yapi/blob/59bade3a8a43e7db077d38a4b0c7c584f30ddf8c/server/install.js#L1-L33","documentation":"The YApi installer refuses to run when <runtime>/init.lock already exists, because the lock file marks the instance as already installed and running install again would overwrite data. Deleting init.lock re-enables installation.","triggerScenarios":"Running the install command (server/install.js install()) when yapi.WEBROOT_RUNTIME/init.lock exists — i.e. a previous install completed (or the lock was left behind) and install is invoked again.","commonSituations":"Re-running 'yapi installer' or 'npm run install-server' on an already-initialized deployment; migrating the app dir while keeping runtime files; accidentally re-running setup in CI.","solutions":["If reinstall is intended: stop the server, delete the init.lock file in the runtime directory, re-run install.","If already installed: do nothing — skip the install step; the instance is initialized.","If reinstalling intentionally, back up the database first (init/seed may overwrite admin account)."],"exampleFix":"// before\nrm -rf nothing; npm run install-server  # fails with this error\n// after\nrm ./yapi/runtime/init.lock && npm run install-server","handlingStrategy":"validation","validationCode":"const fs = require('fs');\nconst lockPath = require('path').join(runtimeDir, 'init.lock');\nconst alreadyInstalled = fs.existsSync(lockPath);\nif (alreadyInstalled) { /* skip install or delete lock deliberately */ }","typeGuard":null,"tryCatchPattern":"try {\n  runInstall();\n} catch (e) {\n  if (/init.lock/.test(e.message)) {\n    console.log('Already installed; skipping.');\n  } else { throw e; }\n}","preventionTips":["Make install steps idempotent in deploy scripts (check init.lock first).","Only run installer on fresh environments.","Back up the database before deleting init.lock for a reinstall."],"tags":["installation","lock-file","idempotency"],"backgroundTag":"already-installed-lock-file","analyzedSha":"59bade3a8a43e7db077d38a4b0c7c584f30ddf8c","analyzedAt":"2026-08-29T08:45:22.203Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}