-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ESM import in ESM modules results into TypeError: (0 , import_....default) is not a function #2480
Comments
It looks like you're bundling your ESM code into CommonJS (or IIFE), but with "lodash-es" being externalized. (i.e.
The simplest workaround is do not externalize lodash-es, just remove it from your |
I am not sure if I can match all your arguments with the call I have found in the jest preset:
|
That's the cause. While as I know jest cannot run in ESM mode, so you may not be able to use lodash-es with jest. |
We are using it in conjuction with jest-resolve. I was expecting jest-resolve will detect the correct order of the dependencies and will transform first lodash-es before trying to transform / load our library using lodash-es. |
i have a question , why this callExpression second parameter is 1 in esbuild output result ? 🤔️
|
As you can see from the source code, the second parameter of |
i got a issue maybe related "use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(
exports, "__esModule", { value: true });
const Graphemer_1 = __importDefault(require("./Graphemer"));
exports.default = Graphemer_1.default; here is the output result, the var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// 🔧 it will hit this branch to define default props again,
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var import_graphemer = __toESM(require_lib(), 1);
// ❌ runtime error
var gaphemer = new import_graphemer.default(); |
The same problem occurs when using export const exportExcel = () => import('xlsx-js-style').then(xlsx => {}) Workaround: const safeESModule = <T>(a: T | { default: T }): T => {
const b = a as any;
return b.__esModule || b[Symbol.toStringTag] === 'Module' ? b.default : b;
};
export const exportExcel = () => import('xlsx-js-style').then(safeESModule).then(xlsx => {}) |
I have this problem when bundling code for the browser and having
I do: import xs from 'xstream';
console.log(xs.never()); This fails when running in the browser with Removing The problem
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
)); which in turn messes up the default import of I've scanned the excellent docs and read the explanations in https://esbuild.github.io/content-types/#default-interop, but it feels like I'm between a rock and a hard place: I want ESM on the server, but don't want this to affect esbuild's bundling of client code. WorkaroundUsing @ApplY3D's workaround above worked: // safe-xstream.ts
import _xs from "xstream";
const safeESModule = <T,>(a: T | { default: T }): T => {
const b = a as any;
return b.__esModule || b[Symbol.toStringTag] === "Module" ? b.default : b;
};
const xs = safeESModule(_xs);
export default xs; // index.ts
import xs from './safe-xstream';
console.log(xs.never()); Minimal repro// src/index.ts
import xs from 'xstream';
console.log(xs.never()); // package.json
{
"name": "tmp",
"version": "1.0.0",
"type": "module",
"scripts": {
"build": "./build-js.sh",
},
"dependencies": {
"esbuild": "^0.20.1",
"xstream": "^11"
}
} #!/bin/sh
# build-js.sh
set -eo pipefail
npx esbuild \
--color=true \
--bundle \
--target=es2018 \
--format=esm \
--outfile=build/bundle-dev.js \
./src/index.ts <!doctype html>
<html>
<body>
<script src="/bundle-dev.js" type="module"></script>
</body>
</html> |
I have issues with ESM imports in ESM modules. I am using esbuild@0.13.23 but also esbuild@0.15.5 fails.
I think it could be related to #2384 and #2026.
lodash-es/isPlainObject is a .js file.
Here are the specific file parts:
Original mjs file
esbuild output
The text was updated successfully, but these errors were encountered: