这个错误是因为你在非模块文件中使用了 'import' 语句。在 Node.js 中,只有在模块中才能使用 'import' 语句,而不能在普通的脚本文件中使用。

要解决这个问题,你可以将你的代码放在一个模块文件中。你可以创建一个新的 JavaScript 文件,并将你的代码复制到这个文件中。然后,在你的主文件中,使用 'require' 语句来导入这个模块文件。

例如,创建一个名为 'myModule.js' 的新文件,并将你的代码放在其中:

// myModule.js
'import' { someFunction } 'from' './someFile';

// rest of your code here

然后,在你的主文件中,使用 'require' 语句导入这个模块文件:

// main.js
const myModule = 'require'('./myModule');

// rest of your code here

这样就可以避免在非模块文件中使用 'import' 语句而导致的错误。

Node.js 错误: SyntaxError: Cannot use import statement outside a module - 解决方法

原文地址: https://www.cveoy.top/t/topic/phIr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录