繁体   English   中英

即使在 package.json 中指定 "type":"module" 用于导入外部文件,仍然抛出 SyntaxError: Cannot use import statement outside a module

[英]Even specifing "type":"module" in package.json for importing external file, still throwing SyntaxError: Cannot use import statement outside a module

我找不到解决问题的方法。 我使用 VS Code 在 JavaScript 中进行编码。我想从外部文件中导入一个对象数组。 在 my.js 文件中,我使用了这个语句:

import {myarray} from './path/to/file.js'

我在我的 package.json 文件的脚本部分遇到添加“类型”:“模块”的警告。 添加后,我仍然有与错误消息相同的警告: SyntaxError: Cannot use import statement outside a module

在您的 main.js 文件中使用它,从外部文件导入您想要的内容。

import { myarray } from "./path/to/file.js";

然后为您的外部文件

export myarray = [];

将 .js 文件重命名为 .mjs。 希望这有效。

暂无
暂无

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

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