Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 15, 2025

Problem

When running npx karin init to initialize Karin for the first time, the application would crash with the error:

TypeError: data.find is not a function

This prevented users from successfully completing the initial setup, though restarting the terminal would make it work on subsequent attempts.

Root Cause

The issue was in packages/cli-Internal/src/init.ts at line 360, where configuration files were being merged during initialization:

const mergedData = { ...JSON.parse(defData), ...JSON.parse(targetData) }

When both groups.json and privates.json (which are arrays) were merged using the object spread operator, they were inadvertently converted from arrays into objects with numeric keys:

// Before spreading: [{ key: "default" }, { key: "global" }]
// After spreading:  { 0: { key: "default" }, 1: { key: "global" } }

Since objects don't have the .find() method, subsequent code that tried to call data.find(item => item.key === 'global') would fail with the TypeError.

Solution

1. Fixed Array Merging in init.ts

Added proper type detection to preserve array types during config file merging:

const defParsed = JSON.parse(defData)
const targetParsed = JSON.parse(targetData)

// Preserve array types instead of converting to objects
let mergedData
if (Array.isArray(defParsed) && Array.isArray(targetParsed)) {
  mergedData = targetParsed
} else {
  mergedData = { ...defParsed, ...targetParsed }
}

2. Updated Default Configuration Files

Fixed packages/core/default/config/privates.json and groups.json to include:

  • The missing "global" key entry (required by the format functions)
  • The "inherit": true property on all entries (required by TypeScript interfaces)
  • Removed deprecated properties (memberEnable, memberDisable)

3. Added Defensive Checks

Enhanced the format() functions in both privates.ts and groups.ts to gracefully handle edge cases:

const format = (data: Privates): Record<string, PrivatesObjectValue> => {
  // Ensure data is an array before using .find()
  if (!Array.isArray(data)) {
    logger.warn('[privates.json] 配置文件格式错误,使用默认配置')
    data = defaultConfig.privates
  }
  // ... rest of function
}

Testing

Verified the fix with a comprehensive test script that confirms:

  • ✅ Config files are valid JSON arrays with all required keys
  • ✅ Array spreading behavior is corrected
  • ✅ init.ts properly handles array types
  • ✅ Defensive checks prevent future issues with corrupted config data

This fix ensures that first-time initialization with npx karin init will complete successfully without errors.

Fixes #[issue_number]

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • registry.npmmirror.com
    • Triggering command: node /home/REDACTED/.npm/_npx/4876f32048baf8ac/node_modules/.bin/prebuild-install -r napi --pkg_version=5.1.7 --pkg_name=sqlite3 (dns block)
    • Triggering command: node /home/REDACTED/.npm/_npx/c65dbefa27d408e0/node_modules/.bin/prebuild-install -r node --pkg_version=0.12.0 --pkg_name=node-pty-prebuilt-multiarch (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Bug: 使用npx karin init 初始化后首次启动会报错TypeError: data.find is not a function</issue_title>
<issue_description>### 描述问题

karin在首次启动的时候会报错TypeError: data.find is not a function

复现步骤

.

预期行为

.

实际行为

.

操作系统

1

Node.js 版本

20.18.2

项目版本

1.10.22

附加信息

删除终端重新打开后恢复正常</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #527


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: sj817 <74231782+sj817@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix TypeError on initial launch of karin Fix TypeError: data.find is not a function on first startup after npx karin init Oct 15, 2025
Copilot AI requested a review from sj817 October 15, 2025 06:21
@github-actions
Copy link
Contributor

你可以通过以下命令安装该版本:

pnpm add https://pkg.pr.new/KarinJS/Karin/node-karin@3af794b -w

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: 使用npx karin init 初始化后首次启动会报错TypeError: data.find is not a function

2 participants