找不到模块:错误:无法解析“ react-native / Libraries / Image / resolveAssetSource”

时间:2019-01-30 11:54:31

标签: react-native npm webpack

我要在当前的本机项目中添加一个音频模块。我尝试安装几个模块(react-native-soundreact-native-track-player)。在两个模块中获得相同的错误输出,该错误输出始终指向'react-native / Libraries / Image / resolveAssetsource',而未找到该模块。

ERROR in ./node_modules/react-native-track-player/lib/index.js
Module not found: Error: Can't resolve 'react-native/Libraries/Image/resolveAssetsource' in 'D:\workspaces\web\react\blink\node_modules\react-native-track-player\lib'
 @ ./node_modules/react-native-track-player/lib/index.js 1:401-459
 @ ./node_modules/react-native-track-player/index.js
 @ ./components/ui/BlinkAudio/BlinkAudio.web.js
 @ ./components/ui/BlinkAudio/index.web.js
 @ ./components/dialogs/ResourceDetails/ResourceDetails.js
 @ ./components/dialogs/ResourceDetails/index.js
 @ ./components/panels/catalog/CatalogPanel.js
 @ ./components/parts/Main/Main.js
 @ ./components/parts/Main/index.js
 @ ./index.web.js

这是音频模块react-native-track-player的索引文件中的当前导入:

import { Platform, AppRegistry, DeviceEventEmitter, NativeEventEmitter, NativeModules } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetsource';

我已尝试通过以下方式从“ react-native”的导入中解决此问题,包括“ resolveAssetsource”:

import { 
    Platform, 
    AppRegistry, 
    DeviceEventEmitter, 
    NativeEventEmitter, 
    NativeModules, 
    resolveAssetsource 
} from 'react-native';

但是我不确定这是否是最好的方法,通常我不喜欢直接接触包节点模块。

我还试图将音频模块从webpack加载程序中排除,但没有结果。

 module: {
    loaders: [
      {
        test: /\.js?$/,
        exclude: function (modulePath) {
          return (
            /node_modules/.test(modulePath) &&
            !/node_modules(\\|\/)react-native-track-player/.test(modulePath)
          );
        },

我想知道是否有人可以帮助我找到答案,以及是否有可能解决这个react-native问题,因为我认为这些音频模块错误地调用了resolveAssetsource,或者反过来,我不知道,native不会为第三方提供此特定的库。

2 个答案:

答案 0 :(得分:1)

这似乎是语法错误问题。没有名为resolveAsset s ource的文件或模块,模块和文件名为resolveAsset S ource,大写S

尝试使用react-native-track-player删除yarn remove react-native-track-player并重新安装(或删除整个node_modules目录并再次运行yarn install )作为react-native-track-player/lib/index.js下的源代码正确无误:

import { Platform, AppRegistry, DeviceEventEmitter, NativeEventEmitter, NativeModules } from 'react-native';
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';

如果在其他地方使用它,则必须像这样导入它:

import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';

但这不是首选方法,因为此方法是RN Image类/组件Image.resolveAssetSource(source)的成员,因此,最好导入Image组件并像这样使用它:

import { 
    Platform, 
    AppRegistry, 
    DeviceEventEmitter, 
    NativeEventEmitter, 
    NativeModules, 
    Image  // Add Image import
} from 'react-native';

// and use the function as of Image method

let audioAssetSource = Image.resolveAssetSource(audioSource);

无论哪种方式,您都可以使用该方法并解决资产来源信息。

答案 1 :(得分:0)

我遇到了同样的问题。我确保在/node_modules/react-native/Libraries/Image/resolveAssetSource.js中存在resolveAssetSource文件。

在过去的项目中,我只是将import resolveAssetSource from 'resolveAssetSource'放在文件的顶部,它可以正常工作,但现在对我不起作用。我也尝试过使用整个路径导入文件,但存在相同的错误。