Git Product home page Git Product logo

TypeError: Cannot set property X of #<Object> which has only a getter about esbuild HOT 2 OPEN

damusix avatar damusix commented on June 4, 2024
TypeError: Cannot set property X of # which has only a getter

from esbuild.

Comments (2)

evanw avatar evanw commented on June 4, 2024

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.

damusix avatar damusix commented on June 4, 2024

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.