调试器无法找到模块' StyleSheet'。我的应用运行良好。路径点很好

时间:2018-04-01 01:57:55

标签: javascript node.js debugging react-native visual-studio-code

我似乎无法解决的奇怪错误。

React-native和node.js以及VS Code

为了让VS Code达到这个目的,我使用了这条线,纱线添加了babel-cli

错误我得到:

import {
    FlatList,
    View,
    Text,
    ActivityIndicator,
    StyleSheet
} from 'react-native';

奇怪,因为:

  • 我的应用运行良好

  • 如果我注释掉这一行,则找到所有其他导入,只需要 样式表。

  • 所有文件都是我所期望的位置。这似乎有些错误?

home.js(由App.js调用)

 {
     "type": "node",
     "request": "launch",
     "name": "Launch Program",
     "program": "${workspaceFolder}/App.js",
     "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/babel-node",
 },

launch.json

{
"name": "AwesomeProject",
"version": "0.1.0",
"private": true,
"devDependencies": {
"jest-expo": "25.0.0",
"react-native-scripts": "1.11.1",
"react-test-renderer": "16.2.0"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js"
 },
"jest": {
"preset": "jest-expo"
 },
"dependencies": {
"babel-cli": "^6.26.0",
"expo": "^25.0.0",
"react": "16.0.0",
"react-dom": "16.0.0",
"react-native": "0.52.0",
"react-navigation": "^1.5.1",
"react-redux": "^5.0.7",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0",
"save": "^2.3.2"
}
}

的package.json

    path\Property2\AwesomeProject\App.js:1

(function (exports, require, module, __filename, __dirname) { import React, 
 { Component } from 'react';
                                                          ^^^^^^

 SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:152:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)

有什么想法继续挖掘? launch.json文件?连接?纱?还是拼写错误?

更新:

删除了关于babel的行,因为我使用yarn for node.js和Expo在android上运行。错误已更改为:

 warning "expo > react-native-maps@0.19.0" has incorrect peer dependency 
 "react-native@0.51.0".
 warning " > react-native@0.52.0" has incorrect peer dependency 
 "react@16.2.0".
 warning "jest-expo > babel-jest@22.4.3" has unmet peer dependency "babel-
 core@^6.0.0 || ^7.0.0-0".
 [4/4] Rebuilding all packages...
 success Saved lockfile.
 success Saved 1 new dependency.
 └─ jest-expo@25.1.0-beta.0
 warning "jest-expo" is already in "devDependencies". Please remove existing 
 entry first before adding it to "dependencies".

使用纱线升级jest-expo@25.1.0-beta.0

double

2 个答案:

答案 0 :(得分:0)

这是我的反应本机示例项目的最小package.json文件,用于测试您的案例。基本上您使用usage here create-react-native-app )来创建项目,这是 expo客户端。请参阅下面的package.json文件:

{
    "name": "AwesomeProject",
    "version": "0.1.0",
    "private": true,
    "devDependencies": {
      "react-native-scripts": "1.13.1",
      "jest-expo": "26.0.0",
      "react-test-renderer": "16.3.0-alpha.1"
    },
    "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
    "scripts": {
      "start": "react-native-scripts start",
      "eject": "react-native-scripts eject",
      "android": "react-native-scripts android",
      "ios": "react-native-scripts ios",
      "test": "jest"
    },
    "jest": {
      "preset": "jest-expo"
    },
    "dependencies": {
      "expo": "^26.0.0",
      "react": "16.3.0-alpha.1",
      "react-native": "0.54.0"
    }
  }

您无需为基本项目添加 babel-cli 读取其{{3}},但如果仍需要,则将其添加到您的dev依赖项中。你还是不需要 react-dom ,因为它的reactjs项目不是本地的反应。

我的建议是在你开始你的项目之前尝试阅读并逐步进行,然后你就不会陷入一些无关紧要的问题。

答案 1 :(得分:0)

您使用的是哪个节点版本?我问这个是因为在你的问题中你提到了代码抛出的错误

路径\ Property2 \ AwesomeProject \ App.js:1

(function (exports, require, module, __filename, __dirname) { import React, 
 { Component } from 'react';
                                                          ^^^^^^

 SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:152:10)
at Module._compile (module.js:624:28)
at Object.Module._extensions..js (module.js:671:10)

似乎代码在解析导入语句时遇到问题。所以可能是你的babel无法解析这种新语法。