2017-08-11 50 views
1

我創建了一個新的反應原生項目(版本0.47.1)。現在我想寫一個新的組件,用於我的反應原生應用。創建新組件失敗:無法解析模塊

/my-component.jsx

import React from 'react'; 
import {Text} from 'react-native'; 

export default class MyComponent extends React.Component { 
    render() { 
     return <Text>My Component</Text> 
    } 
} 

/index.android.js

import React, { Component } from 'react'; 
import { AppRegistry } from 'react-native'; 
import MyComponent from './my-component'; 

export default class Test extends Component { 
    render() { 
     return (
      <MyComponent /> 
     ); 
    } 
} 

AppRegistry.registerComponent('test',() => Test); 

的打包器引發以下錯誤:

error: bundling failed: "Unable to resolve module `./my-component` from `<project_root_dir>/index.android.js`: 
could not resolve `<project_root_dir>/my-component' as a folder: it did not contain a package, nor an index file" 

添加文件擴展名來導入結果在相同的錯誤,除了與附加到文件擴展名錯誤中的路徑。

同樣的錯誤也適用於通過NPM添加依賴關係。無論如何,錯誤中顯示的路徑都存在。

回答

0

您似乎已將條目文件例如index.android.js移動到自定義目錄中,即test =>(test/index.android.js)。爲了這個目的,它無法找到項文件。再次放置index.android.js到根目錄將解決這個問題。

如果你想改變你可以參考以下項目的進入點: https://github.com/facebook/react-native/issues/3442

Change entry file path of android and ios in react-native project

+0

我很抱歉,我想我顯示的文件結構,以錯誤的方式。根文件夾是'test'。不管怎麼說,還是要謝謝你! –

相關問題