{"record":{"id":"2e59f436c8032b4a","repo":"libnyanpasu/clash-nyanpasu","slug":"no-default-export-or-main-function","errorCode":null,"errorMessage":"no default export or main function","messagePattern":"no default export or main function","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"backend/tauri/src/enhance/script/js.rs","lineNumber":396,"sourceCode":"        visitor.visit_program(&result.program);\n        #[cfg(test)]\n        eprintln!(\"visitor: {:#?}\", visitor);\n        if visitor.default_export.is_some() {\n            return Ok(Cow::Borrowed(script));\n        }\n        // check whether `function main` exists\n        match visitor\n            .declared_functions\n            .iter()\n            .find(|(name, _)| name.contains(\"main\"))\n        {\n            Some((_, span)) => {\n                // just insert `export default` before the function\n                let mut script = script.to_string();\n                script.insert_str(span.start as usize, \"export default \");\n                Ok(Cow::Owned(script))\n            }\n            None => Err(anyhow::anyhow!(\"no default export or main function\")),\n        }\n    }\n}\n\n#[cfg(test)]\nmod test {\n    #[tokio::test]\n    async fn yaml_template_preserves_nested_config_through_runner() {\n        use super::{super::runner::Runner, JSRunner};\n\n        let runner = JSRunner::try_new().unwrap();\n        let input = serde_yaml::from_str(\"existing: true\").unwrap();\n        let script = r#\"\n            import { yaml } from 'nyan:utils';\n            export default function main(config) {\n                config.a = yaml`nested:\n  b: 1\n  c: 2","sourceCodeStart":378,"sourceCodeEnd":414,"githubUrl":"https://github.com/libnyanpasu/clash-nyanpasu/blob/f7dbce2997c633e484f54788035e770b3ee99773/backend/tauri/src/enhance/script/js.rs#L378-L414","documentation":"After parsing, wrap_script_if_not_esm looks for either an `export default` statement or a function named `main` to serve as the script entry point. If neither is found, it cannot wrap the script and throws this error.","triggerScenarios":"Submitting an enhancement JS script with no `export default ...` and no top-level `function main(...)` - e.g. only side-effect code or an entry function under a different name.","commonSituations":"Users write helper-only scripts, name the entry function something other than `main`, or write CommonJS `module.exports` scripts.","solutions":["Add `function main(config) { ... return config; }` to the script.","Or add `export default function main(config) { ... }` for ESM-style scripts.","Rename the entry function to exactly `main` if it exists under another name."],"exampleFix":"// before\nfunction run(config) { return config; }\n// after\nfunction main(config) { return run(config); }","handlingStrategy":"validation","validationCode":"/(export\\s+default|function\\s+main\\s*\\()/.test(scriptSource) || fail('script needs export default or main()')","typeGuard":null,"tryCatchPattern":"try {\n  await saveScript(src);\n} catch (e) {\n  if (e.message.includes('no default export or main function')) {\n    promptUserToAddMainFunction();\n  } else throw e;\n}","preventionTips":["Always define `function main(config)` as the entry point","Use templates that already include a main function","Avoid module.exports-only CommonJS style"],"tags":["javascript","scripting","missing-export"],"backgroundTag":"missing-required-config-field","analyzedSha":"f7dbce2997c633e484f54788035e770b3ee99773","analyzedAt":"2026-09-08T01:24:59.197Z","contentChangedAt":"2026-09-08T01:24:59.197Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}