unjs / consola

🐨 Elegant Console Logger for Node.js and Browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ESM version?

sachinraja opened this issue · 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.

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

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)
commented

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

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...')

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.