Teaching DevEco about Bun

How a HarmonyOS IDE type-checks, hides and navigates your backend files.

3 MIN READView Markdown ↗

Your backend lives in src/main/ets, next to the screens. That is great for you and confusing for DevEco: it type-checks every .ts there with its own TypeScript 4.9, and it resolves packages from oh_modules, never node_modules. Out of the box, import { Database } from 'bun:sqlite' is a red line.

A bridge through a relative path#

The ArkAPI library that DevEco installs ships a server.d.ts. DevEco resolves arkapi/server to it, and it points straight back at your project's real packages:

Diagram
 <project>/
 ├── node_modules/ ◄──────────────────────────────┐
 │   ├── arkapi/        the real server API       │
 │   └── bun-types/, @types/node/                 │ 5 × ../
 └── oh_modules/.ohpm/<id>/oh_modules/arkapi/     │
     └── server.d.ts ─────────────────────────────┘

 server.d.ts:
   /// <reference path="../../../../../node_modules/bun-types/index.d.ts" />

Five levels up from that file is always your project root, so a relative path reaches node_modules without DevEco ever searching it. The file also adds a few overloads TypeScript 4.9 needs for Bun's web and SQLite types. Only the editor reads it; Bun and the build use the npm package.

If ohpm ever installs the library in a different layout, generate stops with unsupported ohpm layout instead of leaving you with silently broken types (cli/src/editor-types.ts).

Editing your build file without breaking it#

The global Bun and Node declarations must be listed in buildOption.arkOptions.types of the module's build-profile.json5. That file is JSON5, with comments and your own settings. init does not rewrite it: it parses it with the TypeScript parser, finds the exact array, and inserts only the missing entries. Your comments, order and formatting stay.

Hiding what you should not edit#

Every api file has a generated *.api.ets twin right next to it, and it has to sit there. A screen writes import { addNote } from './notes.api': ArkTS resolves that line to .ets before .ts, and Bun resolves the same line to your .ts file (From signature to client). The twin cannot move to a hidden folder, so ArkAPI hides it from view instead.

Diagram
 On disk                             In DevEco's project tree
 pages/                              pages/
 ├── Notes.ets                       ├── Notes.ets
 ├── notes.api.ts                    └── notes.api.ts
 └── notes.api.ets   (generated)

init writes three things into the DevEco project files (cli/src/idea.ts):

  • The module's .iml gets <excludePattern pattern="*.api.ets" />. It is one pattern, not a list of files, so an api file you add tomorrow is hidden too. If modules.xml or the .iml does not exist yet, init creates them, so the setting is in place before DevEco first opens the project.
  • workspace.xml gets "show excluded files" turned off. An existing true is switched to false; nothing else in the file changes.
  • Your root .gitignore stops ignoring .idea, so the exclude pattern reaches everyone who clones the project. .idea/.gitignore keeps the per-user files out: /workspace.xml, /shelf/ and /.deveco/. That means the "show excluded files" switch stays each developer's own view setting.

Running init again changes nothing, because each edit first checks whether it is already there. DevEco keeps these settings when it syncs.

Command-click to the backend#

The optional DevEco plugin (ide/deveco) changes one thing: Command-click on addNote(...) in a screen opens the backend function in notes.api.ts, not the generated client.

Diagram
 Notes.ets             notes.api.ets               notes.api.ts
 addNote(this.draft) ──► (generated, skipped) ──► export function addNote(...)
          ⌘-click

It runs before Huawei's own handler and only redirects calls into generated ArkAPI clients. It finds the function by name in the live editor tree, so it works with unsaved edits, aliases and async functions; anything else falls through to the normal "go to declaration".

Tab to navigate results · Enter to open · Esc to close