微信小程序报错: SyntaxError: Cannot use import statement outside a module

微信小程序数据绑定,导包出现了:

"SyntaxError: Cannot use import statement outside a module"

排查问题步骤记录,共勉

1.出现问题代码:

css 复制代码
import {createStoreBindings} from "mobx-miniprogram-bindings"

import {store} from "../../store/store"

2.解决问题

  • 2.1. 使用const {} = require(" ")代替import {} from ' '
css 复制代码
  //原代码:
  
  import {createStoreBindings} from "mobx-miniprogram-bindings"
  
  import {store} from "../../store/store"

  //替换代码:
  
  //没报错
  const {createStoreBindings} = require("mobx-miniprogram-bindings") 
  
  //报"找不到文件或者变量"
  const {store} = require("../../store/store")  

注:const {} = require() 不能彻底解决问题,换种方法

  • 2.2.点击详情(右上角),选择本地设置,将js编译成es5 选项 选中,重新编译下

3.结论:

在这篇文章中,我们了解了"SyntaxError:无法在模块之外使用 import 语句"的错误以及如何修复它

更具体描述:这是当您尝试在同一项目中混合使用 ESM 和 CommonJS 模块系统时引起的

要解决此问题:

1.您必须继续使用 CommonJS 模块系统并使用关键字require

2.整个项目完全切换到 ESM 模块系统并使用关键字import

相关推荐
show43319 小时前
2026小程序端视频转文字技术对比:识别引擎精度实测
小程序·音视频
sam-zy1 天前
微信小程序+ESP8266+Arduino 开发智能插座,有本地OTA功能
微信小程序·小程序
A24207349301 天前
微信小程序开发:常用基础语法与指令详解
微信小程序·小程序·notepad++
黄华SJ520it1 天前
青蓝送水类型社交裂变小程序系统技术拆解
小程序·系统开发
weikecms2 天前
星巴克API接口,星巴克点餐接口开发
大数据·小程序
游戏开发爱好者82 天前
系统状态:专为iPhone和iPad设计的实时系统性能监控工具全面介绍
android·ios·小程序·uni-app·iphone·webview·ipad
2501_915909062 天前
全面解析iOS应用上架到App Store的完整流程与关键注意事项
android·macos·ios·小程序·uni-app·cocoa·iphone
千舟软件2 天前
千舟软件介绍 | 顺便聊聊我们做了哪些产品
大数据·数据库·科技·小程序·业界资讯
laboratory agent开发2 天前
2026小程序系统多维度技术与落地能力深度解析
微信小程序
2501_915106322 天前
iOS 证书类型及作用说明 账号证书与签名配置的完整解读
android·ios·小程序·https·uni-app·iphone·webview