语法错误:“导入"和“导出"可能仅与"sourceType:模块"一起出现(1:0) [英] SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)

查看:223
本文介绍了语法错误:“导入"和“导出"可能仅与"sourceType:模块"一起出现(1:0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直遇到此错误.它最近发生了几次,现在我不能摆脱它.我在我的React Native项目中使用MobX,所以我在.babelrc中需要一些东西,所以我有装饰器支持:

I keep hitting this error. Its happened a few times recently and now I can't get rid of it. I'm using MobX in my React Native project and so I need something in my .babelrc so I have decorator support:

{
  "presets": ["react-native"],
  "plugins": ["transform-decorators-legacy"]
}

我尝试了一些不同的变化,但都给了我下面的错误.如果删除它,则由于不支持装饰器而收到错误消息.

I've tried a few difference variations but all give me the error below. If I remove it, I get an error due to decorators not being supported.

SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' (1:0)
    at Parser.pp$5.raise (/path-to/node_modules/babylon/lib/index.js:4246:13)
    at Parser.pp$1.parseStatement (/path-to/node_modules/babylon/lib/index.js:1749:16)
    at Parser.pp$1.parseBlockBody (/path-to/node_modules/babylon/lib/index.js:2133:21)
    at Parser.pp$1.parseTopLevel (/path-to/node_modules/babylon/lib/index.js:1645:8)
    at Parser.parse (/path-to/node_modules/babylon/lib/index.js:1537:17)
    at Object.parse$1 [as parse] (/path-to/node_modules/babylon/lib/index.js:6466:37)
    at extractDependencies (/path-to/node_modules/react-native/packager/react-packager/src/JSTransformer/worker/extract-dependencies.js:29:23)
    at transform (/path-to/node_modules/react-native/packager/react-packager/src/JSTransformer/worker/index.js:53:9)
    at module.exports (/path-to/node_modules/react-native/packager/transformer.js:130:3)
    at transformCode (/path-to/node_modules/react-native/packager/react-packager/src/JSTransformer/worker/index.js:27:3)
transformed 35/139 (25%)/path-to/node_modules/babylon/lib/index.js:4249
  throw err;

我尝试删除我的node_modules并重新安装了几次.我在用纱.在再次运行yarn install之前,还删除了锁定文件.

I've tried removing my node_modules and reinstalling a few times. I'm using yarn. Also removed the lock file before running yarn install again.

我遵循了本指南来安装Babel插件. 我的package.json看起来像:

I followed this guide to get the Babel plugins installed. My package.json looks like:

  "dependencies": {
    "apsl-react-native-button": "^3.0.0",
    "mobx": "^2.5.0",
    "mobx-react": "^3.5.5",
    "react": "15.3.2",
    "react-native": "0.37.0",
    "react-native-autogrow-textinput": "^2.0.3",
    "react-native-awesome-button": "^1.6.0",
    "react-native-couchbase-lite": "git://github.com/adamski/react-native-couchbase-lite.git#gradle-experimental",
    "react-native-keep-awake": "git://github.com/adamski/react-native-keep-awake.git#experimental-gradle",
    "react-native-keyboard-aware-view": "^0.0.11",
    "react-native-keyboard-dodging-view": "git://github.com/adamski/react-native-keyboard-dodging-view#upper-text-inputs",
    "react-native-modalbox": "^1.3.4",
    "react-native-navigation": "git://github.com/adamski/react-native-navigation.git#gradle-experimental-update",
    "react-native-searchbar": "^0.4.2",
    "react-native-simple-store": "^1.0.1",
    "react-native-swipe-list-view": "^0.3.1"
  },
  "devDependencies": {
    "babel-plugin-transform-decorators-legacy": "^1.3.4",
    "babel-preset-react-native": "^1.9.1"
  }

我尝试更新到RN 0.38,但仍然遇到相同的错误.

I tried updating to RN 0.38 but still get the same error.

运行ag babylon会产生

yarn.lock
200:    babylon "^6.11.0"
885:    babylon "^6.11.0"
896:    babylon "^6.11.0"
911:babylon@^6.11.0, babylon@^6.13.0, babylon@^6.13.1:
913:  resolved "https://registry.yarnpkg.com/babylon/-/babylon-6.14.1.tgz#956275fab72753ad9b3435d7afe58f8bf0a29815"
3478:    babylon "^6.13.0"
3555:    babylon "^6.13.1"

这表明不同版本的巴比伦之间存在冲突吗?

which suggests a conflict among different versions of babylon?

任何人都可以告诉我此错误消息的实际含义吗?这是我的代码中发生的事情,还是模块之间的babel版本不匹配?

Can anyone tell me what this error message actually means? Is it something in my code, or is it a mismatch of babel versions among my modules?

推荐答案

我认为对此有一个打包工具,babel-preset-react-native-stage-0

I think there is a package for that, babel-preset-react-native-stage-0

,然后在您的.babelrc中将"react-native-stage-0/decorator-support"添加到预设数组中

and in your .babelrc add "react-native-stage-0/decorator-support" to your presets array

这篇关于语法错误:“导入"和“导出"可能仅与"sourceType:模块"一起出现(1:0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆