reactjs - 发布到 npm 时出现问题

标签 reactjs npm ecmascript-6 create-react-app npm-publish

所以我尝试使用 React 构建一个开源项目并将其推送到 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

如果有人能帮我解决这个问题,那就太好了。提前致谢!

最佳答案

为了将 React 库推送到 NPM,您可能需要一些样板文件来为您安装和转换许多东西。

=====

我还成功地将几个 React 库推送到 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/

关于reactjs - 发布到 npm 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43626034/

相关文章:

reactjs - 将参数传递给 reducer ?

performance - 比如说 Backbone,React-Redux 应用程序真的可以扩展吗?即使重新选择。在移动

node.js - 重复重新安装尝试后,npm run 找不到模块 'sass'

javascript - 无法在 Docker 容器内运行 JavaScript 测试

javascript - ES6 尾调用优化是否涵盖生成器?

javascript - typescript :来自父类(super class)构造函数的成员变量

javascript - Material UI Autocomplete 组件未显示来自 react 状态的值

javascript - React.props.children 未定义

typescript - 在编译 typescript 时找不到“tsc 命令”

javascript - 合并 2 个类