YMFE/yapi · error
init.lock文件已存在,请确认您是否已安装。如果需要重新安装,请删掉init.lock文件
Error message
init.lock文件已存在,请确认您是否已安装。如果需要重新安装,请删掉init.lock文件
What it means
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.
Source
Thrown at server/install.js:15
const fs = require('fs-extra');
const yapi = require('./yapi.js');
const commons = require('./utils/commons');
const dbModule = require('./utils/db.js');
const userModel = require('./models/user.js');
const mongoose = require('mongoose');
yapi.commons = commons;
yapi.connect = dbModule.connect();
function install() {
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
if (exist) {
throw new Error(
'init.lock文件已存在,请确认您是否已安装。如果需要重新安装,请删掉init.lock文件'
);
}
setupSql();
}
function setupSql() {
let userInst = yapi.getInst(userModel);
let passsalt = yapi.commons.randStr();
let result = userInst.save({
username: yapi.WEBCONFIG.adminAccount.substr(0, yapi.WEBCONFIG.adminAccount.indexOf('@')),
email: yapi.WEBCONFIG.adminAccount,
password: yapi.commons.generatePassword('ymfe.org', passsalt),
passsalt: passsalt,
role: 'admin',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()View on GitHub (pinned to 59bade3a8a)
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).
Example fix
// before rm -rf nothing; npm run install-server # fails with this error // after rm ./yapi/runtime/init.lock && npm run install-server
Defensive patterns
Strategy: validation
Validate before calling
const fs = require('fs');
const lockPath = require('path').join(runtimeDir, 'init.lock');
const alreadyInstalled = fs.existsSync(lockPath);
if (alreadyInstalled) { /* skip install or delete lock deliberately */ } Try / catch
try {
runInstall();
} catch (e) {
if (/init.lock/.test(e.message)) {
console.log('Already installed; skipping.');
} else { throw e; }
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of YMFE/yapi@59bade3a8a (2026-08-29).
Data as JSON: /api/errors/22329d98d22954bf.
Report an issue: GitHub.