coffeescript icon indicating copy to clipboard operation
coffeescript copied to clipboard

Bug: coffee command doesn't work with "type": "module" in package.json

Open johndeighan opened this issue 3 years ago • 3 comments

Choose one: is this a bug report or feature request? Bug report

Input Code

import {say} from './src/lib/coffee_utils.coffee'
say "Hello, World!"

Expected Behavior

Outputs "Hello, World!" to the console

Current Behavior

Error message: (node:4316) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.

Now, that error is clearly coming from nodejs; however, I suspect that the real problem is with how the coffee command uses nodejs. The error message from nodejs is wholly unuseful because 1) it suggests doing something I've already done (put "type": "module" in my package.json) and 2) suggests doing something that isn't an option (my files are coffee files, so I can't use the .mjs extension). Maybe the coffee command can, however, let nodejs know that this is an ES module???

Possible Solution

Context

I use ES modules everywhere. But the coffee command can't run those files.

Environment

  • CoffeeScript version: 2.5.1
  • Node.js version: 16.5.0

johndeighan avatar Jul 20 '21 13:07 johndeighan

Hi @johndeighan. One way would be to compile .coffee files before importing. Here is an example.

ch1c0t avatar Jul 20 '21 13:07 ch1c0t

Hi @johndeighan. I’m largely responsible for "type": "module" existing in Node.js. We created it primarily to handle the use case of .coffee and .ts files that need some way other than file extension to signal to Node that they should be treated as ES modules. I lead Node’s loaders group, which is working on enabling a coffeescript/register-like experience for ES modules in Node.

The short version is that there isn’t anything CoffeeScript can do until Node’s loaders feature is more developed. It is being actively worked on, albeit slowly, and once it advances enough to support what CoffeeScript needs then I’ll create a CoffeeScript loader, probably bundled into the CoffeeScript project like register is.

As for the coffee command, it relies on Node’s vm module to evaluate JavaScript, and Node’s vm doesn’t currently support ESM JavaScript other than via --experimental-vm-modules which is unstable and not actively developed at the moment. coffee could be reimplemented to launch a child process of Node using a CoffeeScript loader, though, so there might be a way to solve both register and coffee through the new loaders functionality.

GeoffreyBooth avatar Jul 22 '21 04:07 GeoffreyBooth

Duplicate of https://github.com/jashkenas/coffeescript/issues/5018.

GeoffreyBooth avatar Jul 22 '21 04:07 GeoffreyBooth