【问题标题】:Debugger cannot find module 'StyleSheet'. My app runs fine. Path points fine调试器找不到模块“样式表”。我的应用程序运行良好。路径点很好
【发布时间】:2018-04-01 01:57:55
【问题描述】:

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

React-native 和 node.js 和 VS Code

为了让 VS Code 走到这一步,我使用了以下代码:yarn add babel-cli

我遇到的错误:

Debugger listening on ws://127.0.0.1:39061/61f6ad51-d255-463a-a0ad-b686be9b8b0d
Object {dataReducer: Object, reducerSPY: Object, userApp: Object}
home.js:31
Error: Cannot find module 'StyleSheet'
module.js:555
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.get StyleSheet [as StyleSheet] (c:\AndroidEverything\AndroidStudioProjects\Property2\AwesomeProject\node_modules\react-native\Libraries\react-native\react-native-implementation.js:98:29)
    at Object.<anonymous> (c:/AndroidEverything/AndroidStudioProjects/Property2/AwesomeProject/app/components/home.js:195:16)

奇怪因为:

  • 我的应用运行良好

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

  • 所有文件都符合我的预期。这似乎有问题?

home.js(被App.js调用)

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

launch.json

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

package.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"
}
}

有什么想法可以继续挖掘吗?启动.json 文件?连通性?纱?还是拼写错误?

更新:

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

    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)

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

 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".

【问题讨论】:

  • 是基于 expo 的项目还是 react-native cli?
  • 请在您的问题中添加 package.json 文件
  • 我不确定 expo v cli。我使用纱线进行安装。谷歌搜索也无法弄清楚。文件已更新
  • 我在我的 android 上使用 Expo 并运行 yarn 来运行 node.js。我删除了 launch.json 文件中提到 babel cli 的行,现在出现错误。将张贴在正文中。
  • 试试这个命令yarn upgrade jest-expo@25.1.0-beta.0,让我知道结果。

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


【解决方案1】:

这是我刚刚创建的用于测试您的案例的 react native 示例项目的最小 package.json 文件。基本上,您使用CRNA (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,请阅读其 usage here,但如果您仍然需要,请将其添加到您的开发依赖项中。另外 react-dom 你仍然不需要,因为它用于 reactjs 项目而不是 react-native。

我的建议是在开始项目之前尝试阅读并逐步进行,这样您就不会陷入一些不相关的问题。

【讨论】:

  • 要重新启动此过程更新 npm 并使用 'npm install -g create-react-native-app' 重新安装我认为在查找过程中存在不兼容性,但我相信这个构建是索尔。你对从哪里开始有什么建议吗?目标是使用我的 android phone expo、VS code、react-native。我正在阅读有一个我可能会使用的 chrome 调试器解决方案。
  • create-react-native-app 是一个 expo 客户端,是的,您可以访问调试器以获取更多信息 See Here。当您开始使用本机反应时,我的建议是在编写更多代码之前阅读更多内容。
  • 好的。我会阅读更多。等我一切恢复正常后,我会在几天后回来报告。
  • React 开发者工具可以连接调试器。似乎问题出在 VS 代码中,而 babel 并没有帮助我。如果我想出一个让这个在 VS 代码中工作的解决方案,我会发布它。
【解决方案2】:

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

路径\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)

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

【讨论】:

  • 谢谢,让我尝试重建并让调试器在新的应用程序上工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 2020-08-21
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多