错误:无法解析模块“ buffer” React Native

时间:2019-03-18 17:18:54

标签: reactjs react-native

在Android上构建项目时出现错误

  
    

错误:捆绑失败:错误:无法从buffer解析模块E:\SUNNYCLOCK-MOBILE\node_modules\safe-buffer\index.js:模块     Haste模块映射中不存在buffer

  
     

这可能与   https://github.com/facebook/react-native/issues/4968要解决,请尝试   下列:     1.清除值班员手表:watchman watch-del-all。     2.删除node_modules文件夹:rm -rf node_modules && npm install。     3.重置Metro Bundler缓存:rm -rf /tmp/metro-bundler-cache-*npm start -- --reset-cache。 4.删除急速缓存:rm -rf /tmp/haste-map-react-native-packager-*。       在ModuleResolver.resolveDependency(E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ node-haste \ DependencyGraph \ ModuleResolution.js:161:1460)       在ResolutionRequest.resolveDependency(E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ node-haste \ DependencyGraph \ ResolutionRequest.js:91:16)       在DependencyGraph.resolveDependency(E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ node-haste \ DependencyGraph.js:272:4579)       在Dependencies.map.relativePath(E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ DeltaBundler \ traverseDependencies.js:376:19)       在Array.map()       在resolveDependencies(E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ DeltaBundler \ traverseDependencies.js:374:16)       在E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ DeltaBundler \ traverseDependencies.js:212:33       在Generator.next()       在步骤(E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ DeltaBundler \ traverseDependencies.js:297:313)       在E:\ SUNNYCLOCK-MOBILE \ node_modules \ metro \ src \ DeltaBundler \ traverseDependencies.js:297:473   BUNDLE [android,dev] ./index.js▓▓▓▓▓▓▓▓░░░░░░░░51.9%(813/1143),   失败。

我的React本机信息是

Environment:
  OS: Windows 10
  Node: 8.9.1
  Yarn: 1.7.0
  npm: 4.6.1
  Watchman: Not Found
  Xcode: N/A
  Android Studio: Not Found

Packages: (wanted => installed)
  react: 16.3.1 => 16.3.1
  react-native: 0.55.4 => 0.55.4

我该如何解决这个问题

1 个答案:

答案 0 :(得分:1)

尝试一下:

使用缓冲区在文件顶部:

import { Buffer } from 'buffer';
global.Buffer = Buffer; // very important

如果您不需要直接访问Buffer,只需将上面的代码粘贴到App.js文件中

npm install buffer --save离开后