{"record":{"id":"ae87b8f1de74c47b","repo":"Unitech/pm2","slug":"could-not-create-folder","errorCode":null,"errorMessage":"Could not create folder","messagePattern":"Could not create folder","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/Common.js","lineNumber":220,"sourceCode":"\n  ['log', 'out', 'error', 'pid'].forEach(function(f){\n    var af = app[f + '_file'], ps, ext = (f == 'pid' ? 'pid':'log'), isStd = !~['log', 'pid'].indexOf(f);\n    if (af) af = resolveHome(af);\n\n    if ((f == 'log' && typeof af == 'boolean' && af) || (f != 'log' && !af)) {\n      ps = [cst['DEFAULT_' + ext.toUpperCase() + '_PATH'], formated_app_name + (isStd ? '-' + f : '') + '.' + ext];\n    } else if ((f != 'log' || (f == 'log' && af)) && af !== 'NULL' && af !== '/dev/null') {\n      ps = [cwd, af];\n\n      var dir = path.dirname(path.resolve(cwd, af));\n      if (!fs.existsSync(dir)) {\n        Common.printError(cst.PREFIX_MSG_WARNING + 'Folder does not exist: ' + dir);\n        Common.printOut(cst.PREFIX_MSG + 'Creating folder: ' + dir);\n        try {\n          fs.mkdirSync(dir, { recursive: true });\n        } catch (err) {\n          Common.printError(cst.PREFIX_MSG_ERR + 'Could not create folder: ' + path.dirname(af));\n          throw new Error('Could not create folder');\n        }\n      }\n\n    }\n    // PM2 paths\n    if (af !== 'NULL' && af !== '/dev/null') {\n      ps && (app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = path.resolve.apply(null, ps));\n    } else if (path.sep === '\\\\') {\n      app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = '\\\\\\\\.\\\\NUL';\n    } else {\n      app['pm_' + (isStd ? f.substr(0, 3) + '_' : '') + ext + '_path'] = '/dev/null';\n    }\n    delete app[f + '_file'];\n  });\n\n  return app;\n};\n","sourceCodeStart":202,"sourceCodeEnd":238,"githubUrl":"https://github.com/Unitech/pm2/blob/31adee8048dbbc1ee36a7df7cdbcaca2559708df/lib/Common.js#L202-L238","documentation":"When PM2 resolves out_file / error_file / log file paths for an app, it ensures the parent directory exists by calling fs.mkdirSync(dir, {recursive:true}). If that call throws (permission denied, read-only filesystem, invalid path, disk full), the error is surfaced as 'Could not create folder'.","triggerScenarios":"Configuring an app with out_file/error_file pointing into a directory PM2 cannot create — unwritable location, read-only rootfs, Windows invalid drive, or a path the daemon user can't write to.","commonSituations":"Apps run as a low-privilege user that logs into /var/log without write permission; containers with a read-only root filesystem; path typos; log dir on an unmounted volume.","solutions":["Point log paths to a directory the PM2 daemon user can write to.","Pre-create the log directory and grant write access to the daemon user.","Drop the out_file/error_file overrides so PM2 uses its default log location under PM2_HOME."],"exampleFix":"// before\nout_file: '/var/log/myapp/out.log'\n// after\nout_file: '/home/app/logs/out.log'","handlingStrategy":"try-catch","validationCode":"const fs = require('fs');\nconst path = require('path');\nfunction ensureLogDir(file) {\n  const dir = path.dirname(path.resolve(file));\n  try { fs.mkdirSync(dir, { recursive: true }); return true; }\n  catch (e) { return false; }\n}\nif (!ensureLogDir(app.out_file) || !ensureLogDir(app.error_file)) {\n  delete app.out_file; delete app.error_file; // fall back to PM2 defaults\n}","typeGuard":null,"tryCatchPattern":"try {\n  pm2.start(app, cb);\n} catch (e) {\n  if (/Could not create folder/.test(e.message)) {\n    delete app.out_file; delete app.error_file;\n    pm2.start(app, cb);\n  } else { throw e; }\n}","preventionTips":["Verify the PM2 daemon user can write to every configured log path before start.","Pre-create log directories in deployment scripts.","Omit out_file/error_file in production unless you have a writable target dir."],"tags":["logging","filesystem","permissions"],"backgroundTag":null,"analyzedSha":"31adee8048dbbc1ee36a7df7cdbcaca2559708df","analyzedAt":"2026-08-13T03:49:51.765Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}