es6 模块应该可以在 Coffeescript 中工作(请参阅 https://coffeescript.org/#modules ),但即使是一个非常简单的项目,它对我来说也不起作用。我:
创建了一个新目录
在其中运行“npm init -y”
在我的 package.json 中添加了键 "type": "module"
创建了 2 个文件:index.coffee 和 utils.coffee
从“utils.coffee”导入 {myprint}
myprint(“你好,世界!”)导出 myprint = (str) ->
控制台.log(str)
当我尝试执行index.coffee(通过“coffee index.coffee” - 我已经在 Windows 和 PowerShell 上尝试过 git 的 bash shell)时,我收到以下错误消息:
(node:1856) 警告:要加载 ES 模块,请在 package.json 中设置 "type": "module"或使用 .mjs 扩展名。
及以后:
语法错误:无法在模块外部使用 import 语句
最佳答案
据我所知,这都是正确的。 Coffeescript 正确转换导入/导出语句,但不提供运行它们的环境。
来自documentation :
Note that the CoffeeScript compiler does not resolve modules; writing an import or export statement in CoffeeScript will produce an import or export statement in the resulting output. It is your responsibility to transpile this ES2015 syntax into code that will work in your target runtimes.
您将无法通过 coffee
或 node --require Coffeescript/register
运行此代码。我相信你必须转译你的代码并通过 node
关于node.js - 在coffeescript中使用es6模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67759943/