javascript Browserify 错误:解析文件,意外令牌

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31302786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:37:04  来源:igfitidea点击:

Browserify Error: Parsing file, Unexpected token

javascriptnode.jsnpmreactjsbrowserify

提问by ApathyBear

I am trying to use this npm modulewith browserify.

我正在尝试将此npm 模块与 browserify一起使用。

When I run $ browserify build/widget.js -o bundle.js, I recieve the following error:

当我运行时$ browserify build/widget.js -o bundle.js,我收到以下错误:

Error: Parsing file /Users/nir/browsewidget/node_modules/react-spin/src/main.js: Unexpected token (29:6)
    at Deps.parseDeps (/usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:436:28)
    at fromSource (/usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:375:44)
    at /usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:369:17
    at ConcatStream.<anonymous> (/usr/local/lib/node_modules/browserify/node_modules/concat-stream/index.js:36:43)
    at ConcatStream.emit (events.js:129:20)
    at finishMaybe (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_writable.js:460:14)
    at endWritable (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_writable.js:469:3)
    at ConcatStream.Writable.end (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_writable.js:436:5)
    at DuplexWrapper.onend (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:537:10)
    at DuplexWrapper.g (events.js:199:16)

Note: the file build/widget.jsis not JSX, it has been built using the JSX compiler.

注意:该文件build/widget.js不是 JSX,它是使用JSX 编译器构建的。

Why would I be receiving unexpected token?

为什么我会收到意外的令牌?

Edit based on snozza's answer:

根据 snozza 的回答进行编辑:

I have installed npm install reactify --save.

我已经安装了npm install reactify --save.

Then I ran % browserify -t reactify build/widget.jsWhich gave the -bash: fg: %: no such job

然后我跑了% browserify -t reactify build/widget.js哪个给了-bash: fg: %: no such job

Then I tried browserify -t reactify build/widget.js, which gave:

然后我尝试了browserify -t reactify build/widget.js,它给出了:

Error: Parsing file /Users/nir/browsewidget/node_modules/react-spin/src/main.js: Unexpected token (29:6)
    at Deps.parseDeps (/usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:436:28)
    at fromSource (/usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:375:44)
    at /usr/local/lib/node_modules/browserify/node_modules/module-deps/index.js:369:17
    at ConcatStream.<anonymous> (/usr/local/lib/node_modules/browserify/node_modules/concat-stream/index.js:36:43)
    at ConcatStream.emit (events.js:129:20)
    at finishMaybe (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_writable.js:460:14)
    at endWritable (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_writable.js:469:3)
    at ConcatStream.Writable.end (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_writable.js:436:5)
    at DuplexWrapper.onend (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:537:10)
    at DuplexWrapper.g (events.js:199:16)

Here is a snippet of my build/widget.jsas requested:

这是我build/widget.js的要求的片段:

    var React =require('react');
    var Spinner = require('react-spin')

    var Loading = React.createClass({displayName: "Loading",
        render: function() {
            var spinCfg ={
              lines: 5 // The number of lines to draw
              , length: 5 // The length of each line
              , width: 42 // The line thickness
              , radius: 21 // The radius of the inner circle
              , scale: 1 // Scales overall size of the spinner
              , corners: 1 // Corner roundness (0..1)
              , color: '#000' // #rgb or #rrggbb or array of colors
              , opacity: 0.25 // Opacity of the lines
              , rotate: 0 // The rotation offset
              , direction: 1 // 1: clockwise, -1: counterclockwise
              , speed: 1 // Rounds per second
              , trail: 60 // Afterglow percentage
              , fps: 20 // Frames per second when using setTimeout() as a fallback for CSS
              , zIndex: 2e9 // The z-index (defaults to 2000000000)
              , className: 'spinner' // The CSS class to assign to the spinner
              , top: '50%' // Top position relative to parent
              , left: '50%' // Left position relative to parent
              , shadow: false // Whether to render a shadow
              , hwaccel: false // Whether to use hardware acceleration
              , position: 'absolute' // Element positioning
            };

            return React.createElement(Spinner, {config: spinCfg})
        }
    })

//...etc...

Any ideas?

有任何想法吗?

回答by snozza

Looking through the main.js file of react-spin https://github.com/thomasboyt/react-spin/blob/master/src/main.js, it does indeed contain JSX syntax, namely:

查看 react-spin https://github.com/thomasboyt/react-spin/blob/master/src/main.js的 main.js 文件,确实包含 JSX 语法,即:

return (
      <span ref="container" />
);

That is leading to the parse error when browserify is parsing that file. You could use a transformer such as https://www.npmjs.com/package/reactifyin conjunction with browserify to transform the JSX into vanilla JS.

当 browserify 解析该文件时,这会导致解析错误。您可以将转换器(例如https://www.npmjs.com/package/reactify)与 browserify 结合使用,将 JSX 转换为 vanilla JS。

EDIT: Reactify example

编辑:反应示例

As it is a required node_module that also needs to be transformed, you will need to add the browserify/reactify transform option to the package.json of react-spin. Go to the react-spinfolder and copy this into the package json, beneath "main":

由于它是必需的 node_module 也需要转换,因此您需要将 browserify/reactify 转换选项添加到 .json 的 package.json 中react-spin。转到react-spin文件夹并将其复制到包 json 中,在下面"main"

"browserify": {"transform": ["reactify"]},

Then try run the browserify command once again

然后再次尝试运行 browserify 命令