mobxjs/mobx · warning

Failed to find mobx import, can't add makeObservable as depe

Error message

Failed to find mobx import, can't add makeObservable as dependency in ${fileInfo.path}

What it means

After transforming decorators into `makeObservable` calls, the codemod adds `makeObservable` to the mobx import specifiers. If the file has no value import from 'mobx', it cannot add the dependency and prints a console warning; the generated code may then reference makeObservable without importing it.

Source

Thrown at packages/mobx-undecorate/src/undecorate.ts:260

            createConstructor(clazz, members, privates)
            needsInitializeImport = true
        }

        // rewrite all @observer / @inject
        if (!options?.keepDecorators && decoratorsUsed.has("observer")) {
            handleObserverAndInject(clazzPath)
        }
    })

    if (needsInitializeImport && !options?.ignoreImports) {
        // @ts-ignore
        const mobxImport = source
            .find(j.ImportDeclaration)
            .filter(im => im.value.source.value === "mobx")
            .nodes()
            .filter(node => node.importKind === "value")[0]
        if (!mobxImport) {
            console.warn(
                "Failed to find mobx import, can't add makeObservable as dependency in " +
                    fileInfo.path
            )
        } else {
            if (!mobxImport.specifiers) {
                mobxImport.specifiers = []
            }
            if (importOverride) {
                mobxImport.specifiers.push(j.importSpecifier(j.identifier("override")))
            }
            mobxImport.specifiers.push(j.importSpecifier(j.identifier("makeObservable")))
        }
    }
    if (!decoratorsUsed.size && !usesDecorate) {
        return // no mobx in this file
    }
    if (changed) {
        return source.toSource()

View on GitHub (pinned to 01211a698b)

Solutions

  1. Add a value import from 'mobx' to the file before running the codemod: `import { observable } from 'mobx'`.
  2. If you import mobx from an aliased path, temporarily change it to 'mobx' for the codemod run.
  3. After the run, manually add `makeObservable` to the file's imports if the warning appeared.

Example fix

// before
import { observable } from 'mobx/lib/mobx'
// after
import { observable, makeObservable } from 'mobx'
Defensive patterns

Strategy: validation

Validate before calling

if (!/from\s+['"]mobx['"]/.test(src)) {
  throw new Error(`${file}: add a value import from 'mobx' before running mobx-undecorate`)
}

Type guard

function hasMobxValueImport(source) {
  return /import\s+(type\s*)?\{[^}]*\}\s+from\s+['"]mobx['"]/.test(source) &&
    !/import\s+type\s+\{[^}]*\}\s+from\s+['"]mobx['"]/.test(source)
}

Prevention

When it happens

Trigger: Running the codemod on a file whose decorators come from mobx but which lacks `import ... from 'mobx'` (value import) — e.g. the mobx import was removed, aliased differently, or the file relies on a global mobx.

Common situations: Files using a custom bundler global injection, incorrect import path (e.g. 'mobx/lib/mobx'), type-only imports (`import type`), or decorators imported from another package re-exporting mobx.

Related errors


AI-assisted analysis of mobxjs/mobx@01211a698b (2026-08-28). Data as JSON: /api/errors/8d704797398a1aff. Report an issue: GitHub.