Comments (2)
The behavior you are complaining about is an expected part of how ES modules work in JavaScript. For example:
$ node --input-type=module -e 'import * as fs from "fs"; Object.defineProperty(fs, "foo", { value: "bar" })'
file://./[eval1]:1
import * as fs from "fs"; Object.defineProperty(fs, "foo", { value: "bar" })
^
TypeError: Cannot redefine property: foo
at Function.defineProperty (<anonymous>)
at file://./[eval1]:1:34
at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
at async ModuleLoader.eval (node:internal/modules/esm/loader:218:24)
at async loadESM (node:internal/process/esm_loader:34:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)
Node.js v20.10.0
Since esbuild implements the JavaScript specification, esbuild does this too. Specifically the [[Set]]
internal method of a module namespace exotic object in returns false
, which then causes the PutValue
abstract operation to throw a TypeError
exception. So this is how the import
statement is supposed to behave in JavaScript.
The testing libraries that you want to use are likely designed for CommonJS code and are not designed for use with ES modules (see sinonjs/sinon#2168 and thlorenz/proxyquire#217 for example).
from esbuild.
@evanw Looks like it might be an impossibility even with modern tools: https://www.npmjs.com/package/esmock
I'm using tsx
for running typescript, which uses esbuild
in the background.
from esbuild.
Related Issues (20)
- compilerOptions.paths not resolved when tsconfig extends from another package HOT 1
- ES decorators bug with inheritance and --bundle
- react-compiler support HOT 3
- Question: How did you build a JS/TS parser from scratch? HOT 2
- Empty init functions generated for side-effect-free ESM modules HOT 4
- When transform nullish coalescing it transform != null which may lead to wrong result HOT 3
- [css-minifier]Support CSS Anchor Positioning HOT 2
- Generate TypeScript declarations files for TypeScript projects with isolatedDeclarations enabled
- How can I use 'neutral' platform without getting 'The package "x" wasn't found on the file system but is built into node.' error? HOT 2
- DirectAdmin cannot backup application due to hardlink in esbuild HOT 1
- Import attributes should be enabled for `node18` and >= `node20` targets HOT 2
- Code splitting is creating many small unnecessary chunks HOT 1
- Class missing metadata field in certain cases
- Support for the new "${configDir}" template variable coming in TS v5.5.0
- esbuild tried to read "../../.." , in vite , using npm run dev
- Destructuring bug in Safari when renamed parameter property is identical to the function name
- Class binding references in static field initializers should be resolved to the decorated class
- `browsers` field not taken into account when using `exports`. HOT 1
- esbuild doesn't respect noImplicitUseStrict option in tsconfig.json HOT 1
- On-resolve result suffix property also requires path property?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.