{"record":{"id":"0bc0729e55985fc7","repo":"apify/crawlee","slug":"serviceconflicterror-storagebackend-storageback","errorCode":null,"errorMessage":"ServiceConflictError('StorageBackend', storageBackend, this.#storageBackend)","messagePattern":"ServiceConflictError\\('StorageBackend', storageBackend, this\\.#storageBackend\\)","errorType":"exception","errorClass":"ServiceConflictError","httpStatus":null,"severity":"error","filePath":"packages/core/src/service_locator.ts","lineNumber":268,"sourceCode":"                      localDataDirectory: configuration.storageDir,\n                      logger: this.getLogger().child({ prefix: 'FileSystemStorageBackend' }),\n                  })\n                : new MemoryStorageBackend({\n                      logger: this.getLogger().child({ prefix: 'MemoryStorageBackend' }),\n                  });\n        }\n        return this.#storageBackend;\n    }\n\n    setStorageBackend(storageBackend: StorageBackend): void {\n        // Same instance, no need to do anything\n        if (this.#storageBackend === storageBackend) {\n            return;\n        }\n\n        // Already have a different storage backend that was retrieved\n        if (this.#storageBackend) {\n            throw new ServiceConflictError('StorageBackend', storageBackend, this.#storageBackend);\n        }\n\n        this.#storageBackend = storageBackend;\n    }\n\n    getLogger(): CrawleeLogger {\n        if (!this.#logger) {\n            this.#logger = new ApifyLogAdapter(log);\n        }\n        return this.#logger;\n    }\n\n    setLogger(logger: CrawleeLogger): void {\n        if (this.#logger === logger) {\n            return;\n        }\n\n        if (this.#logger) {","sourceCodeStart":250,"sourceCodeEnd":286,"githubUrl":"https://github.com/apify/crawlee/blob/dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c/packages/core/src/service_locator.ts#L250-L286","documentation":"setStorageBackend() throws ServiceConflictError when a different StorageBackend is provided after one has already been set or retrieved. The locator enforces one storage backend per lifetime so all storages (datasets, KV stores, request queues) resolve consistently.","triggerScenarios":"Calling setStorageBackend(backendA) then setStorageBackend(backendB); or letting crawlee initialize the default backend and afterwards injecting a custom one (e.g. MemoryStorage or a cloud backend).","commonSituations":"Mixing MemoryStorage and default filesystem/cloud storage initialization, or switching storage backends mid-run after a dataset/KV store was already accessed.","solutions":["Call setStorageBackend() once, before any storage access or crawler creation.","Use the same StorageBackend instance throughout the process.","If switching backends is required, create a new ServiceLocator and use it when constructing crawlers.","Set the backend via the standard configuration (e.g. CRAWLEE_STORAGE_BACKEND env or Configuration) instead of manual injection after init."],"exampleFix":"// before\nawait Dataset.open(); // default backend initialized\nlocator.setStorageBackend(new MemoryStorage()); // throws\n\n// after\nconst locator = new ServiceLocator();\nlocator.setStorageBackend(new MemoryStorage());\nawait Dataset.open('my-dataset', { serviceLocator: locator });","handlingStrategy":"validation","validationCode":"let existing;\ntry { existing = locator.getStorageBackend(); } catch { existing = null; }\nif (existing && existing !== myBackend) { /* backend already chosen */ }","typeGuard":null,"tryCatchPattern":"try {\n    locator.setStorageBackend(backend);\n} catch (e) {\n    if (e instanceof ServiceConflictError) {\n        backend = locator.getStorageBackend();\n    } else throw e;\n}","preventionTips":["Choose the storage backend (MemoryStorage vs default) once at startup via configuration.","Never open storages before registering a custom backend.","Avoid mixing init paths (env config + manual injection)."],"tags":["storage","dependency-injection","lifecycle"],"backgroundTag":"service-already-configured","analyzedSha":"dbe57fb09ca607ad59dcf998f3925ef9ac3bb26c","analyzedAt":"2026-08-30T22:22:28.328Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}