繁体   English   中英

NodeJS 错误“不能在模块外使用导入语句”

[英]NodeJS error "cannot use import statement outside a module"

我正在尝试在 javascript 文件中导入 tensorflowjs,但出现错误:

import * as tf from '@tensorflow/tfjs';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:983:16)
    at Module._compile (internal/modules/cjs/loader.js:1033:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1103:10)
    at Module.load (internal/modules/cjs/loader.js:914:32)
    at Function.Module._load (internal/modules/cjs/loader.js:822:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1143:12)
    at internal/main/run_main_module.js:16:11

阅读了关于 SO 的其他答案后,我在 index.html 文件中指定了 type = "module" 但它仍然无法正常工作。

感谢您的帮助。

要加载 ES 模块,请在 package.json 中设置 "type": "module"

import * as tf from "@tensorflow/tfjs";

或尝试证明这一点

   // import * as tf from "@tensorflow/tfjs";
    const tf = require('@tensorflow/tfjs');

如评论中所述,您正在浏览器项目中使用节点导入。 一个简单的解决方法是使用带有 tfjs 库的缩小(或非缩小)jS 包。
因此,在 HTML 文件中,你会做

<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs/dist/tf.min.js"></script>

并直接使用tensorflowjs为TF(你不需要import语句)

暂无
暂无

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

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