简体   繁体   English

如何创建npm包进行反应

[英]How to create npm package for react

I've created this great string parser which parse the string and display only 5 lines or approx 1000(1000+ it'll adjust according to word length) characters(whichever is less) on screen. 我创建了这个很棒的字符串解析器,它解析字符串并在屏幕上仅显示5行或大约1000(1000+个字符,它将根据单词长度进行调整)字符(以较少者为准)。 I've published my package on npm but on installing it I get this error: 我已经在npm上发布了我的软件包,但是在安装时却出现此错误:

Module parse failed: C:\xampp\htdocs\gitlab\communityFrontend\node_modules\react-simple-read-more\src\string-parser.js Unexpected token (77:12)
You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (77:12)

here is package.json of that same package Can anyone please help me with this issue? 这是同一包的package.json 。有人可以帮我解决这个问题吗?

The problem you experience is due to the fact that your string--parser is not a pure javascript file. 您遇到的问题是由于字符串解析器不是纯JavaScript文件。 But instead it uses JSX syntax. 但相反,它使用JSX语法。 And that is the reason why it complains about unexpected token such as < in the following line: 这就是为什么它在以下行中抱怨意外令牌(例如< )的原因:

<div style={{width:'100%',wordWrap: 'break-word'}}>

You must transpile your JSX sources to pure javascript before publishing to NPM - so anyone can consume them. 在发布到NPM之前,您必须将JSX源转换为纯JavaScript,这样任何人都可以使用它们。 And surely do not name JSX files with JS extension to avoid such confusions in the future. 并且一定不要用JS扩展名命名JSX文件,以免将来造成此类混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM