consola icon indicating copy to clipboard operation
consola copied to clipboard

ESM version?

Open sachinraja opened this issue 3 years ago • 3 comments

I'm trying to convert my package over to ESM and am unable to because this package does not have an ESM build. Would it be possible to add one?

Nevermind, this is probably something I should figure out on my end.

sachinraja avatar Jul 25 '21 14:07 sachinraja

The problem isn't actually from this package (or any package), it has to do with my build system.

sachinraja avatar Jul 25 '21 19:07 sachinraja

This is an actual bug in Consola. It affects anyone using ESM and is especially annoying for TypeScript users.

Currently TypeScript ESM users must do the following in order to import the package:

import consolaPkg from 'consola';

const { Consola } = consolaPkg as unknown as typeof import('consola');

This code will throw in an ESM environment even though it compiles:

import { Consola } from 'consola';
file:///my-file.mjs:1
import { Consola } from 'consola';
         ^^^^^^^
SyntaxError: Named export 'Consola' not found. The requested module 'consola' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'consola';
const { Consola } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:181:5)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:281:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:65:12)

jonahsnider avatar Nov 30 '21 00:11 jonahsnider

Excuse me, are there plans to support ESM in the future?

myesn avatar May 06 '22 02:05 myesn

For those of you using TypeScript and "type": "module", I'm using this as a workaround:

/imports/consola.ts

/**
 * Hack to make 'consola' import work with "type": "module".
 */
import consola from 'consola'

export default consola as unknown as typeof consola.default

/src/someFile.ts

import consola from '~/imports/consola.js'

consola.info('Seeding database...')

michaelhays avatar Mar 20 '23 18:03 michaelhays

Hi! First v3 prerelease is out with ESM build!

https://github.com/unjs/consola/releases/tag/v3.0.0-1

You can import consola using import { consola } from 'consola' for proper named exports.

Please report any issues if you found with v3 and esm.

pi0 avatar Mar 28 '23 17:03 pi0