dcloudio / uni-app

A cross-platform framework using Vue.js
https://uniapp.dcloud.io
Apache License 2.0
40.1k stars 3.63k forks source link

uni.requireNativePlugin is not a function #2623

Closed keyiis closed 10 months ago

keyiis commented 3 years ago

操作系统win10 x64 开发工具 vscode typescript 3.9.9 nodejs v14.15.0 执行以下命令生成的小程序代码,没有报错

PS D:\project\uniapp> npm run dev:mp-weixin
> uniapp@0.1.0 dev:mp-weixin
> cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch
请注意运行模式下,因日志输出、sourcemap以及未压缩源码等原因,性能和包体积,均不及发行模式。若要正式发布,请点击发行菜单或使用cli发布命令进行发布
小程序各家浏览器内核及自定义组件实现机制存在差异,可能存在样式布局兼容问题,参考:https://uniapp.dcloud.io/matter?id=mp
-  开始编译当前项目至 mp-weixin 平台...Starting type checking service...
Using 1 worker with 2048MB memory limit
|  开始编译当前项目至 mp-weixin 平台...

 DONE  Compiled successfully in 78606ms                                                                                                                                                                      下午8:30:43

No type errors found
Version: typescript 3.9.9
Time: 24259ms
 DONE  Build complete. The dist\dev\mp-weixin directory is ready. Watching for changes...

但在小程序开发工具里报错

页面【node-modules/@dcloudio/uni-ui/lib/uni-transition/uni-transition]错误:
 TypeError: uni.requireNativePlugin is not a function
    at Module.<anonymous> (vendor.js?t=wechat&s=1619613029359&v=09aaa5fd31ceafd63efdad1a105d7256:15517)
    at Module.207 (vendor.js?t=wechat&s=1619613029359&v=09aaa5fd31ceafd63efdad1a105d7256:15637)
    at __webpack_require__ (runtime.js? [sm]:92)
    at Object._ (uni-transition.js? [sm]:122)
    at __webpack_require__ (runtime.js? [sm]:92)
    at Module._ (uni-transition.js? [sm]:100)
    at __webpack_require__ (runtime.js? [sm]:92)
    at Module._ (uni-transition.js? [sm]:13)
    at __webpack_require__ (runtime.js? [sm]:92)
    at Object.nodeModulesDcloudioUniUiLibUniTransitionUniTransitionCreateComponent (uni-transition.js? [sm]:462)

由于程序里使用了uni-popup组件,而这个组件又引用了uni-transition组件,结合错误查看uni-transition组件源码后发现,下面的条件编译其实没有生效,我的理解是编译小程序环境,这段代码不应该被编译进去,但实际是被编译进去了,所以就出现了上面的错误,那么问题应该与小程序开发工具无关,而是uniapp的打包编译过程出了问题,我在另一台电脑使用同样版本的环境(操作系统/vscode/typescript/uniapp/nodejs)却没有问题

// #ifdef APP-NVUE
const nvueAnimation = uni.requireNativePlugin('animation')
// #endif

package.json

"dependencies": {
    "@dcloudio/uni-app-plus": "^2.0.0-31420210305001",
    "@dcloudio/uni-h5": "^2.0.0-31420210305001",
    "@dcloudio/uni-helper-json": "*",
    "@dcloudio/uni-mp-360": "^2.0.0-31420210305001",
    "@dcloudio/uni-mp-alipay": "^2.0.0-31420210305001",
    "@dcloudio/uni-mp-baidu": "^2.0.0-31420210305001",
    "@dcloudio/uni-mp-qq": "^2.0.0-31420210305001",
    "@dcloudio/uni-mp-toutiao": "^2.0.0-31420210305001",
    "@dcloudio/uni-mp-vue": "^2.0.0-31420210305001",
    "@dcloudio/uni-mp-weixin": "^2.0.0-31420210305001",
    "@dcloudio/uni-quickapp-native": "^2.0.0-31420210305001",
    "@dcloudio/uni-quickapp-webview": "^2.0.0-31420210305001",
    "@dcloudio/uni-stat": "^2.0.0-31420210305001",
    "@dcloudio/uni-ui": "^1.3.0",
    "@vue/shared": "^3.0.0",
    "core-js": "^3.6.5",
    "flyio": "^0.6.2",
    "qrcode": "^1.4.4",
    "regenerator-runtime": "^0.12.1",
    "vue": "^2.6.11",
    "vue-class-component": "^6.3.2",
    "vue-moment": "^4.1.0",
    "vue-property-decorator": "^8.0.0",
    "vuex": "^3.2.0"
  },
  "devDependencies": {
    "@babel/plugin-syntax-typescript": "^7.2.0",
    "@babel/runtime": "~7.12.0",
    "@dcloudio/types": "^2.0.24",
    "@dcloudio/uni-automator": "^2.0.0-31420210305001",
    "@dcloudio/uni-cli-shared": "^2.0.0-31420210305001",
    "@dcloudio/uni-migration": "^2.0.0-31420210305001",
    "@dcloudio/uni-template-compiler": "^2.0.0-31420210305001",
    "@dcloudio/vue-cli-plugin-hbuilderx": "^2.0.0-31420210305001",
    "@dcloudio/vue-cli-plugin-uni": "^2.0.0-31420210305001",
    "@dcloudio/vue-cli-plugin-uni-optimize": "^2.0.0-31420210305001",
    "@dcloudio/webpack-uni-mp-loader": "^2.0.0-31420210305001",
    "@dcloudio/webpack-uni-pages-loader": "^2.0.0-31420210305001",
    "@types/qrcode": "^1.4.0",
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-typescript": "*",
    "@vue/cli-service": "~4.5.0",
    "babel-plugin-import": "^1.11.0",
    "cross-env": "^7.0.2",
    "jest": "^25.4.0",
    "mini-types": "*",
    "miniprogram-api-typings": "*",
    "node-sass": "^4.14.1",
    "postcss-comment": "^2.0.0",
    "sass-loader": "^8.0.2",
    "typescript": "^3.9.9",
    "vue-template-compiler": "^2.6.11"
  },
289420843 commented 3 years ago

同样的问题

ilovintit commented 3 years ago

有临时解决方案吗

keyiis commented 3 years ago

@ilovintit 如果是小程序环境,直接注释掉就不会报错了

// #ifdef APP-NVUE
// const nvueAnimation = uni.requireNativePlugin('animation')
// #endif
gzxxy110506 commented 3 years ago

注释了虽然能跑起来了,不过一些uni的组件用不了,像是uni-ui框架中的条件编译全部失效了的样子,像uni-popup就用不了。有官方大佬回复吗

keyiis commented 3 years ago

@gzxxy110506 我是微信小程序环境,注释了没有影响,uni-popup也可以正常使用

gzxxy110506 commented 3 years ago

@keyiis 我也是在微信小程序环境,编译成微信小程序,在微信开发者工具上就会有问题,如果不用cli创建,直接hx跑就是正常的

mehaotian commented 3 years ago

在根目录下新建 vue.config.js ,添加配置

module.exports = {
    transpileDependencies: ['@dcloudio/uni-ui']
}
gzxxy110506 commented 3 years ago

在根目录下新建 vue.config.js ,添加配置

module.exports = {
    transpileDependencies: ['@dcloudio/uni-ui']
}

有用,感谢大佬