cocos creator3.6.1 导入npm包-behavior3js,使用报错

  • Creator 版本:
    3.6.1

  • 目标平台:
    Android

  • 重现方式:
    npm install --save behavior3js

  • 首个报错:
    2023-04-27 11:18:45 [Scene] Did you forget the extension? Please note that you can not omit extension in module specifier.
    Error: [Scene] Did you forget the extension? Please note that you can not omit extension in module specifier.

at logger (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:755:13)

at ExecutorSystem.resolve [as _detailResolve] (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:772:17)

at ExecutorSystem._resolve (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\src\editor-systemjs\index.ts:120:14)

at SystemJS.resolve (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\src\editor-systemjs\index.ts:36:23)

at H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:355:37

at Array.map ()

at H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:353:41

2023-04-27 11:18:45 [Scene] Error: Error: Module “…/constants” not found for file:///H:/Hero/Hero/node_modules/behavior3js/libs/behavior3-0.2.2.js
at rejector (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:759:15)

at ExecutorSystem.resolve [as _detailResolve] (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:777:13)

at ExecutorSystem._resolve (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\src\editor-systemjs\index.ts:120:14)

at SystemJS.resolve (H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\src\editor-systemjs\index.ts:36:23)

at H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:355:37

at Array.map ()

at H:\CocosDashboard\resources.editors\Creator\3.6.1\resources\app.asar\node_modules@editor\lib-programming\static\executor\systemjs-bridge\out\index.js:353:41

Error: [Scene] Error: Error: Module “…/constants” not found for file:///H:/Hero/Hero/node_modules/behavior3js/libs/behavior3-0.2.2.js

  • 之前哪个版本是正常的:
  • 手机型号:
  • 手机浏览器:
  • 编辑器操作系统:
  • 重现概率:

我的ts代码是:
import b3 from ‘behavior3js’;

报错截图:

我的预期应该是跟 npm包-jszip 一样,
import jz from ‘jszip’
直接就能用了。
但是不知道为啥这个behavior3js不行

emm,有人帮忙指导一下吗

image

我可能对npm包的引入不是很熟悉,我改为显示加载也是有同样的问题,下图我贴出了我的代码:

我在使用“recast-navigation”库时也遇到了这个问题,直接使用库名导入
import { NavMesh } from “recast-navigation”;

这样编辑器直接运行可以,Build时就报错

“error”: “Error: Module “module” not found for file:///E:/CodeUp/B01-WJDJ/WJDJProject/node_modules/@recast-navigation/core/dist/index.es.js”

如果导入时使用绝对路径,就会直接报错。

Can not load module e:/CodeUp/B01-WJDJ/WJDJProject/node_modules/recast-navigation/index. Cause: Unknown module.

折腾了快一天了,感觉答案应该就在
https://docs.cocos.com/creator/manual/zh/scripting/modules/example.html
https://docs.cocos.com/creator/manual/zh/scripting/modules/spec.html
里边,可是就是改不对!
/(ㄒoㄒ)/~~

折腾了两天,没辙把JS都重写成TS了,还是不对才发现问题所有~

第三方JS中有一处import(“module”);是区分了条件加载的,见下图:
image

导致了编辑器直接运行就没问题,打包就报错,注释掉就万事大吉了~ :rofl:

楼主,这个问题你后来有解决吗