{"id":324594,"date":"2021-02-06T09:48:26","date_gmt":"2021-02-06T01:48:26","guid":{"rendered":"http:\/\/4563.org\/?p=324594"},"modified":"2021-02-06T09:48:26","modified_gmt":"2021-02-06T01:48:26","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-import-%e4%b8%80%e4%b8%aa%e5%ba%93%e5%90%8e%ef%bc%8c%e5%b0%b1-syntax-error-unexpected-token-export-%e4%ba%86%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=324594","title":{"rendered":"\u4e3a\u4ec0\u4e48 import \u4e00\u4e2a\u5e93\u540e\uff0c\u5c31 syntax error, unexpected token &#8216;export&#8217; \u4e86\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u4e3a\u4ec0\u4e48 import \u4e00\u4e2a\u5e93\u540e\uff0c\u5c31 syntax error, unexpected token &#8216;export&#8217; \u4e86\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <span><i><\/i> 13<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>nodejs, react, typescript<\/p>\n<p>node v14.7.0<br \/> react 17.0<br \/> jest 26.6.3<\/p>\n<p>\u8fd9\u662f\u4e00\u4e2a\u6d4b\u8bd5\uff0c\u4e3b\u8981\u7528\u6765\u6d4b\u8bd5\u5165\u53e3\u6587\u4ef6\u7684\u3002 \u6211\u4e0d\u6e05\u695a\u4e3a\u4ec0\u4e48\u5199\u8fd9\u4e2a\u6d4b\u8bd5\uff0c\u90fd\u662f legacy \uff08\u51e0\u5e74\u524d\u7684\uff0c\u5199\u7684\u4eba\u4e5f\u8d70\u4e86\uff09\u7684\u4ee3\u7801\u3002<br \/> \u4e5f\u4e0d\u592a\u597d\u5220\u9664\uff0c\u5426\u5219\uff0c\u5f97\u53bb\u201c\u89e3\u91ca\u201d\u4e3a\u4ec0\u4e48\u628a\u8fd9\u4e2a\u6d4b\u8bd5\u5220\u9664\u3002\uff08\u5927\u5bb6\u90fd\u61c2\u7684\uff09<\/p>\n<p>\u6211\u7684\u7406\u89e3\uff0c\u8fd9\u4e2a\u6d4b\u8bd5\u4e3b\u8981\u5c31\u662f\uff0c\u770b\u770b\u80fd\u5426\u6b63\u5e38\u628a index module \u7ed9 load \u8fdb\u6765\uff0c\u4e0d\u51fa\u9519\u5c31\u884c\u3002<\/p>\n<pre><code>describe(\"tests\", () =&gt; {   describe(\"bootstraping the app\", () =&gt; {     it(\"test react render method\", () =&gt; {       const app = document.createElement(\"div\");       app.setAttribute(\"id\", \"app\");       document.body.appendChild(app);        const bootStrapper = () =&gt; {         require(\"..\/index\");       };        expect(bootStrapper).not.toThrowError();        document.body.removeChild(app);     });   }); }); <\/code><\/pre>\n<p>\u7136\u540e\uff0cindex.tsx \u6587\u4ef6\uff0c\u5c31\u662f\u6211\u4eec\u7a0b\u5e8f\u7684\u5165\u53e3\u6587\u4ef6\uff0c\u5f15\u7528 react\uff0c\u7136\u540e render app.<\/p>\n<pre><code>import * as React from \"react\"; ... blabla   <\/code><\/pre>\n<p>\u73b0\u5728\uff0c\u6709\u65b0\u7684\u9700\u8981\uff0c\u6211\u6dfb\u52a0\u4e86 \u4e00\u4e2a import <\/p>\n<pre><code>import * as React from \"react\"; import { Inspector } from \"react-dev-inspector\";  &lt;inspector&gt;   &lt;app \/&gt; &lt;\/inspector&gt;  <\/code><\/pre>\n<p>\u7528\u8fd9\u4e2a wrap \u539f\u6765\u7684 app.<\/p>\n<p>\u9879\u76ee\u90fd\u80fd\u591f\u6b63\u5e38\u7f16\u8bd1\uff0c\u8fd0\u884c\u3002\u6240\u6709\u7684\u529f\u80fd\u4f7f\u7528\u4e0b\u6765\u90fd\u6b63\u5e38\u3002\u4f46\u5c31\u662f\u4e0a\u9762\u8fd9\u4e2a\u6d4b\u8bd5\u8dd1\u4e0d\u8fc7\u3002<br \/> \u63d0\u793a\u51fa\u9519\u7684\u4f4d\u7f6e\uff0c\u5c31\u662f\u90a3\u4e2a import \u8bed\u53e5\u90a3\u91cc\u3002<\/p>\n<pre><code>    expect(received).not.toThrowError()      Error name:    \"SyntaxError\"     Error message: \"Unexpected token 'export'\" <\/code><\/pre>\n<p>\u4e0d\u662f\u5f88\u6e05\u695a\uff0c\u662f module \u5bfc\u51fa\u6709\u95ee\u9898\uff0c\u8fd8\u662f import \u6709\u95ee\u9898\u3002<br \/> google \u4e86\u534a\u5929\uff0c\u6ca1\u6709\u627e\u5230\u7c7b\u4f3c\u95ee\u9898\u6216\u8005\u5408\u9002\u7684\u7b54\u6848\u3002<\/p>\n<p>\u7279\u6765\u8bf7\u6559\u4e00\u4e0b\u3002 \u8c22\u8c22<\/p>\n<p>p.s. https:\/\/stackoverflow.com\/questions\/38296667\/getting-unexpected-token-export<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5009984\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yaphets666 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             import { Inspector } from &#8220;react-dev-inspector&#8221;; <br \/>\u8bd5\u4e0b\u628a\u5927\u62ec\u53f7\u53bb\u6389<br \/>import Inspector from &#8220;react-dev-inspector&#8221;;                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009985\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yaphets666 <br \/>\u6211\u67e5\u770b\u4e86\u4e00\u4e0b react-dev-inspector \u7684\u6587\u4ef6 index.d.ts \uff0c\u5c31\u4e00\u884c\u3002<br \/>&#8220;`<br \/>export * from &#8216;.\/Inspector&#8217;;<br \/>&#8220;`<br \/>\u6240\u4ee5\uff0c\u4f60\u8bf4\u7684\u65b9\u6cd5\u4e0d\u884c\u3002\u4e0d\u8fc7\uff0c\u6211\u8bd5\u4e86\u4e00\u4e0b<\/p>\n<p>import * as devinspector from &#8220;react-dev-inspector&#8221;;<br \/>blabla&#8230;<\/p>\n<p>yarn start \u80fd\u591f\u7f16\u8bd1\u901a\u8fc7\uff0c\u7a0b\u5e8f\u80fd\u8fd0\u884c\u3002\u4f46\u662f\uff0ctest \u8fd8\u662f\u8fc7\u4e0d\u4e86\u3002\u9519\u8bef\u548c\u524d\u9762\u4e00\u6837\u7684\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009986\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : magicdawn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u9879\u76ee\u8dd1\u8d77\u6765\u548c\u6d4b\u8bd5\u4e0d\u662f\u4e00\u4e2a\u914d\u7f6e\u5457.<br \/>\u5efa\u8bae\u68c0\u67e5 tsconfig.json compileOptions.module \/ babel \u76f8\u5173\u7684 includes                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009987\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : alan0liang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e94\u8be5\u662f\u6ca1\u6709\u8bbe\u597d babel-loader \u7684 excludes \u5427\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u6211\u8bb0\u5f97 babel-loader \u4f1a\u8df3\u8fc7\u8f6c\u8bd1 node_modules\uff0c\u9700\u8981\u81ea\u5df1\u5728 webpack config \u91cc\u8bbe\u4e00\u4e0b<\/p>\n<p>#1 \u4e0d\u5bf9\uff0c\u8fd9\u4e2a\u5305\u7528\u7684\u786e\u5b9e\u662f named export \u800c\u4e0d\u662f default export\uff0c\u9700\u8981\u82b1\u62ec\u53f7                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009988\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : yazoox <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @alan0liang <br \/>\u9879\u76ee\u8fd0\u884c\u9700\u8981\u7f16\u8bd1\uff0c\u6240\u4ee5\u4f1a\u8dd1 webpack\uff0c\u4f46\u662f jest \u597d\u50cf\u6ca1\u6709\uff0c\u76f4\u63a5\u8fd0\u884c\u7684<br \/>\u6211\u4eec\u6709\u4e2a jest.config.js \u7684\u914d\u7f6e\u6587\u4ef6 \uff0c\u91cc\u9762\u5012\u662f\u6709 babel \u76f8\u5173<\/p>\n<p>module.exports = {<br \/> preset: &#8220;ts-jest&#8221;,<br \/> testEnvironment: &#8220;jsdom&#8221;,<br \/> reporters: [&#8220;default&#8221;, &#8220;jest-junit&#8221;],<br \/> globals: {<br \/> &#8220;ts-jest&#8221;: {<br \/> babelConfig: {<br \/> plugins: [&#8220;dynamic-import-node&#8221;, &#8220;babel-plugin-styled-components&#8221;]<br \/> },<br \/> tsconfig: &#8220;&lt;rootDir&gt;\/tsconfig.jest.json&#8221;<br \/> }<br \/> }<br \/>}<\/p>\n<p>@magicdawn <br \/>tsconfig.jest.json \u53ea\u6bd4\u9879\u76ee\u8fd0\u884c\u7684 jest \u6587\u4ef6\u591a\u4e0b\u9762\u8fd9\u4e00\u9879\u914d\u7f6e<\/p>\n<p>{<br \/> &#8220;extends&#8221;: &#8220;.\/tsconfig.json&#8221;,<br \/> &#8220;compilerOptions&#8221;: {<br \/> &#8220;esModuleInterop&#8221;: true<br \/> }<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009989\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zthxxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yazoox jest \u5982\u679c\u8981\u8dd1 jsx \/ tsx \u6587\u4ef6\uff0c\u6700\u597d\u4e5f\u7f16\u8bd1\u4e00\u4e0b\uff0c\u4fdd\u8bc1\u548c webpack loader \u5904\u7406\u540e\u4e00\u81f4\uff0c\u5982 jest \u6709 transform \u5b57\u6bb5\u914d\u7f6e\uff0c\u901a\u5e38\u4f1a\u914d\u7f6e ts-jest \u548c babel<\/p>\n<p>https:\/\/jestjs.io\/docs\/en\/configuration#transform-objectstring-pathtotransformer&#8211;pathtotransformer-object                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009990\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zthxxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div> <img decoding=\"async\" src=\"http:\/\/4563.org\/wp-content\/uploads\/2021\/02\/20210206_601ef1d91a11d.png\" rel=\"noreferrer\" alt=\"\u4e3a\u4ec0\u4e48 import \u4e00\u4e2a\u5e93\u540e\uff0c\u5c31 syntax error, unexpected token 'export' \u4e86\uff1f\"><\/p>\n<p>https:\/\/github.com\/zthxxx\/react-dev-inspector\/issues\/14#issuecomment-764200981                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5009991\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zthxxx <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6216\u8005\u5728 jest \u73af\u5883\u4e0b\u5c31\u4e0d\u53bb import \u8fd9\u4e2a\u5e93\u5462\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5e93\u5bfc\u51fa\u7684\u7ed9 react \u7684\u7ec4\u4ef6\u662f es module \u7c7b\u578b\u7684 (\u5bfc\u51fa\u7684\u7ed9 node \u7528\u7684 plugin \u8fd9\u4e9b\u624d\u662f commonjs \u7684)\uff0c\u6240\u4ee5\u5728\u4f60\u7684 jest \u4e0b\u5982\u679c\u4e0d\u7ecf\u8fc7 babel \/ esm \u8fd9\u4e9b\u8f6c\u6362\u5c31\u4e0d\u652f\u6301                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3a\u4ec0\u4e48 import \u4e00\u4e2a\u5e93\u540e\uff0c\u5c31&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/324594"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=324594"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/324594\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=324594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=324594"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=324594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}