Skip to content
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

How can I turn off 'Cannot assign to import" If libs contain it? #891

Closed
mvolkov-prog opened this issue Feb 26, 2021 · 7 comments
Closed

Comments

@mvolkov-prog
Copy link

image

@mvolkov-prog mvolkov-prog changed the title How can I turn off this rule? How can I turn off 'Cannot assign to import" If libs contain it? Feb 26, 2021
@zeallot
Copy link

zeallot commented Feb 26, 2021

Subscribe, have a same problem.

@vkaboskin
Copy link

OH, i have a same problem too. Will watch what to do.

I wanna write my webpack project to esbuild.

@ghost
Copy link

ghost commented Feb 26, 2021

This problem was blocking me, I had to go back to webpack

@evanw
Copy link
Owner

evanw commented Feb 26, 2021

It is not possible to turn this off. This is not valid ECMAScript module code. Properties of an imported module namespace object cannot be assigned to in ECMAScript. Here's an example of trying to do this in node:

$ cat ./example.mjs
import * as vec2 from './example.mjs'
vec2.angle = function (v1, v2) {
};
$ node ./example.mjs
vec2.angle = function (v1, v2) {
           ^

TypeError: Cannot add property angle, object is not extensible

This bundler follows ECMAScript semantics just like node, so if your code uses ECMAScript module syntax and doesn't run in node, then it likely won't run in esbuild either. I think you will likely have to go back to Webpack to be able to use this library.

@Jarred-Sumner
Copy link
Contributor

Jarred-Sumner commented Feb 26, 2021

You could work around it by importing the non-ESM version of the library doing this, or transform it to CommonJS and then import the CommonJS version and esbuild will transform it back

@evanw
Copy link
Owner

evanw commented Feb 27, 2021

Yes, that's a good point. This module ships with broken ESM code but esbuild is only loading it because it's respecting the module field instead of the main field in package.json. You can tell esbuild to not respect the module field using --main-fields=browser,main. More documentation about this API option is here: https://esbuild.github.io/api/#main-fields.

@evanw
Copy link
Owner

evanw commented Mar 3, 2021

I'm going to close this because I think the above answers should be sufficient. Feel free to add additional comments if you have additional thoughts.

@evanw evanw closed this as completed Mar 3, 2021
harellevy added a commit to harellevy/ngx-mapbox-gl that referenced this issue Oct 17, 2023
fixing es-build angular 16 build with error
```
Cannot assign to import "baseApiUrl"

    node_modules/ngx-mapbox-gl/fesm2022/ngx-mapbox-gl.mjs:34:25:
      34 │                 MapboxGl.baseApiUrl = options.customMapboxApiUrl;
         ╵                          ~~~~~~~~~~

  Imports are immutable in JavaScript. To modify the value of this import
```
related to evanw/esbuild#891,



fixes:
Wykks#403
Wykks#407
Wykks pushed a commit to Wykks/ngx-mapbox-gl that referenced this issue Nov 1, 2023
fixing es-build angular 16 build with error
```
Cannot assign to import "baseApiUrl"

    node_modules/ngx-mapbox-gl/fesm2022/ngx-mapbox-gl.mjs:34:25:
      34 │                 MapboxGl.baseApiUrl = options.customMapboxApiUrl;
         ╵                          ~~~~~~~~~~

  Imports are immutable in JavaScript. To modify the value of this import
```
related to evanw/esbuild#891,



fixes:
#403
#407
denisyilmaz pushed a commit to denisyilmaz/ngx-mapbox-gl that referenced this issue Sep 10, 2024
fixing es-build angular 16 build with error
```
Cannot assign to import "baseApiUrl"

    node_modules/ngx-mapbox-gl/fesm2022/ngx-mapbox-gl.mjs:34:25:
      34 │                 MapboxGl.baseApiUrl = options.customMapboxApiUrl;
         ╵                          ~~~~~~~~~~

  Imports are immutable in JavaScript. To modify the value of this import
```
related to evanw/esbuild#891,



fixes:
Wykks#403
Wykks#407
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants