“import” unexpected token? (chrome 62)

后端 未结 1 1012
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-11 21:20

While trying to troubleshoot why systemjs didn\'t find a custom library I installed (could be a follow up question) I was stuck when trying to do things \"manually\".

<
相关标签:
1条回答
  • 2021-01-11 21:56

    You're correct that you need type="module" on your script tag:

    <script src="hi.js" type="module"></script>
    <!-- ---------------^^^^^^^^^^^^^       -->
    

    You also need a ./ prefix on your module specifier:

    import * as hi from "./hi2.js";
    // ------------------^^
    

    This is to leave the door open for a specifier with no path at all to have special meaning at some stage as things evolve. From the WHAT-WG spec:

    This restriction (that a relative URL specifier must start with /, ./, or ../ – T.J.) is in place so that in the future we can allow custom module loaders to give special meaning to "bare" import specifiers, like import "jquery" or import "web/crypto". For now any such imports will fail, instead of being treated as relative URLs.

    When I make those two changes to your files, with Chrome 62 with no experimental flags set, I get the alert.

    0 讨论(0)
提交回复
热议问题