2017-04-26 74 views
2

因此,我嘗試使用react並將其推送到npm來構建開源項目。問題是,我的組件雖然在測試環境中工作良好,但可以安裝到其他組件,但是當我將它發佈到npm並下載包並嘗試訪問它時,它會給我一個錯誤。發佈到npm的問題

下面是代碼

import React, {Component} from 'react'; 
import {Nav, NavBar, NavLink, NavItem} from 'react-bootstrap'; 


class GitNav extends Component{ 
    handleSelect(eventKey){ 
     window.location = this.props.NavURLs[eventKey]; 
    } 
    render(props){ 
    const NavTextItems = this.props.NavTexts.map((eachNav, key) => 
     <NavItem eventKey={key} href="#">{eachNav}</NavItem> 
    ); 
    return(
     <Navbar fixedBottom collapseOnSelect> 
      <Navbar.Header> 
      <Navbar.Toggle /> 
      </Navbar.Header> 
      <Navbar.Collapse> 
       <Nav onSelect={this.handleSelect.bind(this)}> 
       {NavTextItems} 
       </Nav> 
      </Navbar.Collapse> 
      </Navbar> 
    ); 
    } 
} 

export default GitNav; 

這裏的一個小樣本的錯誤:

Error in ./~/react-github-nav/index.js 
Module parse failed: /Users/theawesomeguy/Desktop/Projects/resume3/resume/node_modules/react-github-nav/index.js Unexpected token (11:6) 
You may need an appropriate loader to handle this file type. 
SyntaxError: Unexpected token (11:6) 
@ ./src/App.js 20:22-49 

如果有人可以幫助我走出這一點,那將是巨大的。提前致謝!

+0

你的package.json和webpack文件是什麼樣的? –

+0

我沒有任何這些。事情是,我正在開發它並通過create-react-app進行測試。可能應該提到這一點。我看了@ thinhvo0108的答案,我認爲這是要走的路。儘管感謝您的幫助。這些都是需要考慮的很好的一點。 –

回答

2

爲了將反應庫推入NPM,您可能需要一些樣板文件,它可以爲您安裝和轉換很多東西。

=====

我也推一些反應庫成功打入NPM:

https://www.npmjs.com/~thinhvo0108

https://www.npmjs.com/package/react-sticky-dynamic-header

https://www.npmjs.com/package/react-ringing-bell

=====

你的GitHub庫文件夾結構也應該像雷:

https://github.com/thinhvo0108/react-sticky-dynamic-header

https://github.com/thinhvo0108/react-ringing-bell

=====下面

有用的教程在這裏:

(樣板源)https://github.com/juliancwirko/react-npm-boilerplate

(作者的文章)http://julian.io/creating-react-npm-packages-with-es2015/

+0

這非常有幫助。謝謝您的幫助! –

+1

不客氣,很高興你發現它很有趣^^ – thinhvo0108