swc-project/swc · error

Could not find .swcrc file while using rootMode "upward". Se

Error message

Could not find .swcrc file while using rootMode "upward".
Searched from: {}

What it means

With `rootMode: "upward"`, swc walks parent directories from the input file looking for a .swcrc and treats 'not found' as fatal, bailing with the search start path in the message (crates/swc/src/lib.rs). Other rootModes silently fall back to defaults, which is why this error is specific to upward mode.

Source

Thrown at crates/swc/src/lib.rs:754

            let root = root.as_ref().unwrap_or(&CUR_DIR);

            let swcrc_path = match config_file {
                Some(ConfigFile::Str(s)) => Some(PathBuf::from(s.clone())),
                _ => {
                    if *swcrc {
                        if let FileName::Real(ref path) = name {
                            // Canonicalize relative paths for proper parent traversal
                            let abs_path = if path.is_relative() {
                                root.join(path).canonicalize().ok()
                            } else {
                                path.canonicalize().ok()
                            };
                            let found = abs_path.and_then(|p| find_swcrc(&p, root, *root_mode));

                            // "upward" mode requires a .swcrc to be found
                            if found.is_none() && *root_mode == RootMode::Upward {
                                bail!(
                                    "Could not find .swcrc file while using rootMode \
                                     \"upward\".\nSearched from: {}",
                                    path.display()
                                );
                            }

                            found
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                }
            };

            let config_file = match swcrc_path.as_deref() {
                Some(s) => Some(load_swcrc(s)?),

View on GitHub (pinned to 5176682b65)

Solutions

  1. Create a .swcrc in the project root (or nearest directory covering all compiled files)
  2. Or change rootMode to 'nearest'/'none' if falling back to defaults is acceptable
  3. Verify the traversal start: run from the project directory and prefer absolute input paths

Example fix

# before
swc --sync --config-file .swcrc ./src -d dist   # rootMode upward, no .swcrc above ./src
# after
echo '{ "jsc": { "parser": { "syntax": "typescript" } } }' > .swcrc
swc ./src -d dist
Defensive patterns

Strategy: validation

Validate before calling

// JavaScript - replicate the upward search before running swc
import fs from 'node:fs';
import path from 'node:path';
function findSwcrcUpward(startFile, root) {
  let dir = path.resolve(root, path.dirname(startFile));
  for (;;) {
    const candidate = path.join(dir, '.swcrc');
    if (fs.existsSync(candidate)) return candidate;
    const parent = path.dirname(dir);
    if (parent === dir) return null;
    dir = parent;
  }
}
// if (rootMode === 'upward' && !findSwcrcUpward(file, root)) throw new Error('no .swcrc upward');

Prevention

When it happens

Trigger: Compiling with swcrc: true and rootMode 'upward' where no .swcrc exists anywhere between the file's directory and the filesystem root - e.g. files outside the project (tmp dirs, generated scratch), or relative paths canonicalized against a wrong root/CWD.

Common situations: Monorepo tooling forcing rootMode upward while compiling files above the package root; running the compiler from a different working directory so upward traversal starts in the wrong place; .swcrc renamed or excluded from the deploy image.

Related errors


AI-assisted analysis of swc-project/swc@5176682b65 (2026-08-17). Data as JSON: /api/errors/9bd1c50e015160cb. Report an issue: GitHub.