【问题标题】:Unexpected token (*)意外的标记 (*)
【发布时间】:2018-09-15 18:28:37
【问题描述】:

这行代码给我带来了问题,

import * as posenet from '@tensorflow-models/posenet'

我得到这个错误'Uncaught SyntaxError: Unexpected token *',

我正在使用最新版本的 chrome 并安装了 npm 包“@tensorflow-models/posenet”。我正在使用 npm 包“http-server”来本地主机我的页面。

【问题讨论】:

  • 你可能需要 babel 来编译?
  • 如果浏览器正在编译这个js文件,你需要在script标签中输入type="module"

标签: javascript npm import


【解决方案1】:

你不能像这样直接导入 npm 包,因为浏览器不知道你服务器上的包存储在哪里。

在浏览器中,您可以直接使用其路径导入javascript 文件,即from 'some/file.js'。此外,您的代码必须在模块类型脚本元素中才能使用导入/导出语法。比如

<script type="module">
  import * as someName from 'some/file.js';
</script>

【讨论】:

  • 这是一个节点模块,所以我要导入哪个特定文件?
【解决方案2】:

在节点中使用 TS 时,请在 tsconfig.json 中指定正确的 module。

例如,

{
  "compilerOptions": {
    "module": "commonjs",
    ...
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 2018-08-02
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2020-01-27
    • 2022-01-26
    相关资源
    最近更新 更多