�鸿蒙报错速查:Cannot find name 'require',禁用 require(),根因 + 真解法

报错原文

vbnet 复制代码
ERROR: 10505001 ArkTS Compiler Error
Error Message: Cannot find name 'require'. Did you mean 'Require'? At File: xxx.ets:N:N

报错触发场景

你写鸿蒙 ArkTS 时,用 Node.js 的 require() 就炸:

typescript 复制代码
// ❌ 报错写法
const path = require('path')
const fs = require('fs')
const http = require('@ohos.net.http')

根因

鸿蒙 ArkTS 禁用 Node.js 的 require() ------这是跟 Node.js 最大的差异。Node.js 里 require() 是 CommonJS 规范的模块加载,ArkTS 按 ES Module 规范设计只能用 import。

ArkTS 这么设计的原因:ES Module 是静态分析友好的 ------import 在编译期就能解析依赖图,require() 是运行时动态加载编译器分析不了。ArkTS 要求编译期消除一切歧义,禁用 require()。

真解法

用 import 替代 require(),三种姿势:

解法 1:import 整个模块

typescript 复制代码
// ✅ 正解 1:import 整个模块替代 require()
import common from '@ohos.app.ability.common'
import relationalStore from '@ohos.data.relationalStore'

// 用法
const ctx: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
const rdb: relationalStore.RdbStore = await relationalStore.getRdbStore(ctx, config)

解法 2:import { } 解构单个符号

typescript 复制代码
// ✅ 正解 2:import { } 解构替代 require()
import { AppStorageV2 } from '@ohos.arkui.StateManagement'
import { BusinessError } from '@ohos.base'

// 用法
const cfg = AppStorageV2.connect(AppConfig, 'appConfig', () => new AppConfig())

解法 3:import type 仅类型引入

typescript 复制代码
// ✅ 正解 3:import type 仅类型引入替代 require()
import type { Want } from '@ohos.app.ability.Want'

// 用法(仅类型注解,不占运行时)
const want: Want = { bundleName: 'com.xx', abilityName: 'Main' } as Want

真机配图:import 替代 require 正解能编译能跑

import / import { } / import type 三种替代 require()------正解,能编译能跑。三个函数都真返了正确类型值:

import 正解初始态(import common/import relationalStore/import type Want 均未调用):

点调三个函数后(import common=common.UIAbilityContext,import relationalStore=relationalStore.RdbStore,import type Want=Want.type):

报错写法(用 require())编译就炸,装不上真机;正解写法(import / import { } / import type 替代)能跑,三种替代都真返了正确类型值。用了 require 就炸,import 就跑------这是 ArkTS ES Module 规范最直白的证据。

高频踩坑场景

场景 1:Node.js 习惯写 require('path')

typescript 复制代码
// ❌ 报错(Node.js 习惯)
const path = require('path')
const joined = path.join('a', 'b')

// ✅ 正解(鸿蒙沙箱内不用 path.join,用字符串拼接或 ohos.file.fs)
const joined = 'a' + '/' + 'b'

场景 2:动态加载 require(variable)

typescript 复制代码
// ❌ 报错(require 不能动态加载)
const mod = require动态名

// ✅ 正解(import 静态加载所有可能的模块)
import modA from './modA'
import modB from './modB'
const mod = condition ? modA : modB

场景 3:require 拿 Node.js 内置模块

typescript 复制代码
// ❌ 报错(Node.js 内置模块鸿蒙没有)
const fs = require('fs')
const http = require('http')

// ✅ 正解(鸿蒙对应模块用 import)
import fs from '@ohos.file.fs'
import http from '@ohos.net.http'

场景 4:require 拿相对路径模块

typescript 复制代码
// ❌ 报错
const utils = require('./utils')

// ✅ 正解(import 相对路径)
import { helper } from './utils'

一句话速查

Cannot find name 'require' → 禁用 require(),用 import / import { } / import type 替代

跟 Node.js 的差异

写法 Node.js ArkTS
const x = require('mod') ✅ CommonJS ❌ 报错
import x from 'mod' ✅ ESM ✅
import { x } from 'mod' ✅ ESM ✅
import type { T } from 'mod' ✅ ESM ✅
require(变量) 动态加载 ✅ ❌ 报错

Node.js 转鸿蒙最容易踩这个坑------肌肉记忆写 require(),ArkTS 直接编译炸。新项目从一开始就养成「禁用 require,只用 import」的习惯,避坑。

require 三种替代速查表

替代方案 适用场景 写法示例
import 整模块 拿整个模块 import http from '@ohos.net.http'
import { } 解构 拿单个符号 import { AppStorageV2 } from '@ohos.arkui.StateManagement'
import type 仅类型注解 import type { Want } from '@ohos.app.ability.Want'

铁律 :ArkTS 里搜不到 require 关键字------遇到「要拿模块」就 import / import { } / import type,别想 require()。

完整代码仓库

本文所有正解写法都已托管到 AtomGit:

🔗 仓库地址 :atomgit.com/JaneConan/a...

仓库包含:

  • 四种高频踩坑场景的 ❌ 报错写法 + ✅ 正解写法对照
  • import / import { } / import type 三种替代方案示范
  • 可直接用 DevEco Studio 打开参考

作者:JaneConan 仓库:atomgit.com/JaneConan/a... 协议:Apache-2.0,随便用,别告我

相关推荐
余槐i38 分钟前
PostgreSQL 函数索引报错 must be immutable:3种绕法与代价
数据库·后端·sql·postgresql·性能优化
需要8261 小时前
Spring Bean 生命周期:从实例化到销毁的完整钩子
java·后端·spring
凤山老林2 小时前
Spring Boot 接入国密 SM2/SM3/SM4 的一些实践记录
spring boot·后端·python
伯恩bourne3 小时前
Spring Boot Bean 注册的两种方式:@Component 家族 vs @Configuration + @Bean
java·spring boot·后端
张彦峰ZYF3 小时前
停顿短不等于延迟低:G1、ZGC 与低延迟服务的目标分野、代价账本与选型标定
后端·回收暂停、应用停止总时长·到达安全点、分配停滞·屏障吃吞吐、并发线程吃核心·并发窗口吃堆余量·分区式靠挑回收集兑现目标·染色指针式不支持压缩引用
pride.li4 小时前
第03章 Python 教程-基础语法:注释与变量
服务器·前端·后端·python·算法·计算机视觉
upala4 小时前
【仓颉语言入门 · 第二篇 第 6 课】
后端
罗丹丹Luna4 小时前
移动端显示“已提交”以后:SAP 与低代码集成该怎样暴露真实状态
后端·架构
码事漫谈4 小时前
因为太想讨好你,所以AI会骗你
后端
吴凡wudianmei4 小时前
每调一次模型都花钱?如何用工程手段把调用压到最少?
前端·后端·架构