1# ArkTS Modular Loading 2 3 4## Error Messages Related to Modular Loading Displayed at the Runtime of ArkTS Applications 5### "Cannot find dynamic-import module 'xxxx'" 6 7This error indicates that the module to load is not compiled into the application package. 8 9**Possible cause** 10 11An expression is dynamically loaded as an input parameter, but the module path is incorrect. 12``` typescript 13 import(module).then(m=>{m.foo();}).catch((e: Error)=>{console.info(e.message)}); 14``` 15 16**Locating method** 17 18Print the path information of the module, and check whether the path is correct. 19 20### "Cannot find module 'xxxx' , which is application Entry Point" 21This error indicates that the entry file is not found during application startup. 22 23**Possible cause** 24 25The entry file is not found during application startup. 26 27**Locating method** 28 29(1) Open the application's project-level build file **module.json5** in the **entry/src/main** directory. 30 31 32The following is an example of some parameters in **module.json5**. 33``` 34{ 35 "module": { 36 "name": "entry", 37 "type": "entry", 38 ... 39 "abilities": [ 40 { 41 "name": "EntryAbility", // Module name. 42 "srcEntry": "./ets/entryability/EntryAbility.ts", // Relative path of the src directory to the project root directory. 43 "description": "$string:EntryAbility_desc", 44 "icon": "$media:icon", 45 "label": "$string:EntryAbility_label", 46 "startWindowIcon": "$media:icon", 47 "startWindowBackground": "$color:start_window_background", 48 "exported": true, 49 "skills": [ 50 { 51 "entities": [ 52 "entity.system.home" 53 ], 54 "actions": [ 55 "action.system.home" 56 ] 57 } 58 ] 59 } 60 ] 61 } 62} 63``` 64(2) Check the value of **srcEntry** under **abilities** in **module.json5**. This parameter specifies the path of the entry file. 65 66### "No export named 'xxxx' which exported by 'xxxx'" 67This error indicates that no specific object is found in the module when the .so file in the HAP or HAR of the application is being loaded. 68 69**Possible cause** 70 71The dependency between modules is pre-parsed in the static build phase of the module. If the imported variable name in the .ets file is incorrect, an error message is displayed in DevEco Studio. An error message is also displayed in the application build phase. Note that the dependency of native C++ modules in the application is checked only at runtime. 72 73**Locating method** 74 75Check whether the .so file in the application contains the exported variable that causes the error, and compare the exported variable with the imported variable in the .so file. If they are inconsistent, modify the variable. 76 77 78## Failure in Loading .so Files 79 80If a .so file fails to be loaded, a JS error indicating loading failure is not explicitly thrown. You can check whether the exported object is **undefined** to determine the loading status of the .so file. 81 82**Loading failure symptom** 83 84| Loading Type| TS/JS Module| System Library/Application .so File| 85| -------- | -------- | -------- | 86| Static loading| The VM automatically throws an error and the process exits.| No error is thrown, and the loaded object is **undefined**.| 87| Dynamic loading| No error is proactively thrown and the program reaches the reject branch. You can call the **catch** method to capture the error.| No error is proactively thrown and the program reaches the resolve branch. You can check whether the variable exported by the module is **undefined** in the branch.| 88 89**Example 1: Static loading of the system library/application .so file fails.** 90 91``` 92import testNapi from 'libentry.so' 93 94if (testNapi == undefined) { 95 console.error('load libentry.so failed.'); 96} 97``` 98 99Command output: 100``` 101load libentry.so failed. 102``` 103 104**Example 2: Dynamic loading of the system library/application .so file fails.** 105 106``` 107import('libentry.so') 108 .then(m => { 109 if (typeof m.default === 'undefined') { 110 console.warn(`load libentry.so failed.`); 111 } else { 112 console.info('load libentry.so success:', m); 113 } 114 return m; 115 }) 116 .catch((e: Error) => { 117 console.error('load libentry.so error:', e); 118 }); 119``` 120 121Command output: 122``` 123load libentry.so failed. 124``` 125 126**Possible cause, locating method, and solution** 127 128For details, see [Node-API FAQs](https://gitee.com/openharmony/docs/blob/master/en/application-dev/napi/use-napi-faqs.md). 129 130 131## Initialization Errors at Runtime Caused by Inter-Module Circular Dependency 132 133Inter-module circular dependencies are usually caused by these modules referencing each other's files. 134```ts 135// file1 136 export {b} from './file2' 137 export {a} from './A' 138 139// file2 140 import {a} from './file1' 141 export let b:number = a; 142 143// A 144 export let a:number = 50; 145``` 146The preceding code reports the following error: 147``` 148Error message:a is not initialized 149``` 150 151If only the loading sequence of **file1** is changed: 152```ts 153// file1 154 export {a} from './A' 155 export {b} from './file2' 156// file2 157 import {a} from './file1' 158 export let b:number = a; 159// A 160 export let a:number = 50; 161``` 162The preceding code does not report an error. In this case, the execution order is as follows: (1) **file1** loads module A, and module A has no dependencies on other modules. (2) After execution, **file1** is returned. (3) **file2** is loaded. (4) When **file2** loads **file1**, it needs to access the variable a which is defined in **file1**. Since module A in **file1** has already been executed, **file2** can be loaded normally. 163Note that modular build uses depth-first loading. 164 165### Solution to Circular Dependencies 166[@security/no-cycle](https://developer.huawei.com/consumer/en/doc/harmonyos-guides-V5/ide_no-cycle-V5) 167 168 169## Example Error Scenarios Because ArkTS Symbols Are Not Initialized 170 171The ArkTS coding specification is a subset of the ECMAScript specification. When a symbol that has not been initialized is accessed, an error is thrown during runtime. The following common error scenarios are provided to help you locate and rectify source code errors: 172 173### Access Before const/let Declaration 174 175``` typescript 176console.log(a); // Error: Variable 'a' is used before being assigned. 177console.log(b); // Error: Variable 'b' is used before being assigned. 178 179let a = '1'; 180const b = '2'; 181``` 182**Recommended** 183 184``` 185let a = '1'; 186const b = '2'; 187 188console.log(a); 189console.log(b); 190``` 191 192 193### Instantiation Before Class Declaration 194 195``` typescript 196let a = new A(); // Error: Class 'A' used before its declaration. 197 198class A {} 199``` 200 201**Recommended** 202 203``` 204class A {} 205 206let a = new A(); 207``` 208 209### Accessing Static Properties of a Class Before Declaration 210 211``` typescript 212let a = A.a; // Error: Class 'A' used before its declaration. 213 214class A { 215 static a = 1; 216} 217``` 218 219**Recommended** 220 221``` 222class A { 223 static a = 1; 224} 225 226let a = A.a; 227``` 228 229### Accessing let and const That Are Not Declared in a Function 230 231``` typescript 232foo(); // Error: Error message:a is not initialized 233 234let a = 1; 235const b = 2; 236 237function foo() { 238 let v = a + b; 239} 240``` 241 242**Recommended** 243 244``` 245let a = 1; 246const b = 2; 247 248function foo() { 249 let v = a + b; 250} 251 252foo(); 253``` 254 255### Accessing Static Properties of a Class That Is Not Declared in a Function 256 257``` typescript 258foo(); // Error: Error message:A is not initialized 259 260class A { 261 static a = 1; 262} 263 264function foo() { 265 let v = A.a; 266 let w = new A(); 267} 268``` 269 270**Recommended** 271 272``` 273class A { 274 static a = 1; 275} 276 277function foo() { 278 let v = A.a; 279 let w = new A(); 280} 281 282foo(); 283``` 284 285### Inter-Module Circular Dependency - const/let 286 287``` typescript 288// module1.ets 289import { a, b } from './module2' 290 291export let i = 1; 292export let m = a; 293export const j = 2; 294export const n = b; 295 296// --------------------- 297 298// module2.ets 299import { i, j } from './module1' 300 301export let a = i; // Error: Error message:i is not initialized 302export const b = j; // Error: Error message:j is not initialized 303``` 304 305**Solution** 306 307For details, see [Solution to Circular Dependencies](#solution-to-circular-dependencies). 308 309 310### Inter-Module Circular Dependency - Class 311 312``` typescript 313// class1.ets 314import { b } from './class2' 315 316export class A { 317 static a = b; 318} 319 320// --------------------- 321 322// class2.ets 323import { A } from './class1' 324export let b = 1; 325 326const i = A.a; // Error: Error message:A is not initialized 327const j = new A(); // Error: Error message:A is not initialized 328``` 329 330**Solution** 331 332For details, see [Solution to Circular Dependencies](#solution-to-circular-dependencies). 333