question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error with TypeScript config and ESM

See original GitHub issue

Bug description

I’m using TypeScript webpack config, running with ts-node/esm loader It yells at me:

[webpack-cli] file:///Users/cyber/dev/tombo/freezy/src/webpack/webpack.common.ts:6
import { ESBuildMinifyPlugin } from "esbuild-loader";
         ^^^^^^^^^^^^^^^^^^^
SyntaxError: Named export 'ESBuildMinifyPlugin' not found. The requested module 'esbuild-loader' 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 'esbuild-loader';
const { ESBuildMinifyPlugin } = pkg;

If I do

import pkg from 'esbuild-loader';
const { ESBuildMinifyPlugin } = pkg;

Then I get the error:

[webpack-cli] Failed to load '/Users/cyber/dev/tombo/freezy/src/webpack/webpack.prod.ts' config
[webpack-cli] src/webpack/webpack.common.ts:7:9 - error TS2339: Property 'ESBuildMinifyPlugin' does not exist on type '(this: any, source: string) => Promise<void>'.

7 const { ESBuildMinifyPlugin } = pkg;
          ~~~~~~~~~~~~~~~~~~~

Reproduction

import pkg from 'esbuild-loader';
const { ESBuildMinifyPlugin } = pkg;

Node.js package manager

npm

Environment

System:
    OS: macOS 12.0.1
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 25.19 MB / 16.00 GB
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 14.17.5 - /usr/local/bin/node
    Yarn: 1.22.17 - /usr/local/bin/yarn
    npm: 8.2.0 - /usr/local/bin/npm
  npmPackages:
    esbuild-loader: ^2.17.0 => 2.17.0
    webpack: ^5.65.0 => 5.65.0


### Can you contribute a fix?

- [X] I’m interested in opening a pull request for this issue.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
pi0commented, Dec 20, 2021

Hi! Just wanted to mention that we faced a related issue with Nuxt3 as all internals are ESM only. (without typescript also having issues)

A simple workaround: (https://github.com/nuxt/framework/pull/2460)

import esbuildLoader from 'esbuild-loader'

// For plain mjs
const { ESBuildMinifyPlugin } = esbuildLoader

// For typescript
const { ESBuildMinifyPlugin } = (esbuildLoader as unknown as typeof import('esbuild-loader'))

1reaction
privatenumbercommented, Dec 19, 2021

Does setting esModuleInterop in tsconfig.json help?

In any case, happy to add an ESM distribution now that TS is starting to support export maps in 4.6.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - ECMAScript Modules in Node.js - TypeScript
This setting controls whether .js files are interpreted as ES modules or CommonJS modules, and defaults to CommonJS when not set. When a...
Read more >
TypeScript and native ESM on Node.js - 2ality
In this blog post, I'll explain everything you need to know in order to use and produce native ECMAScript modules on Node.js.
Read more >
TypeScript config can't resolve custom paths with ESM enabled
I've been trying to crack this thing down for quite a while now, I want my tsconfig.json paths to be resolved when ESM...
Read more >
tsc-esm-fix - npm
Nightly build TypeScript 4.7 provides experimental esm support. ... src/main/ts/q/u/x/index.ts:1:21 - error TS2835: Relative import paths ...
Read more >
TypeScript - webpack
Let's set up a configuration to support JSX and compile TypeScript down to ES5. ... true and "esModuleInterop" : true in your tsconfig.json...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found