Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
After the import a variable became a constant

package.json

{
  "type": "module"
}

users.js

let users = ["Jack", "Mary"];

export default users;

index.js

import users from './users.js';

users = [];

After executing index.js I get an error:

users = [];
      ^

TypeError: Assignment to constant variable.

Why? The users was clearly defined as a variable not a constant.

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

  import users from './users.js';

is similar to

const users = ...

in that you cannot assign the value afterward. It's not quite the same because the values can change, but they can only be changed from inside the module. so inside users.js

let users = [];

export const setUser(newUsers) {
  users = newUsers
}

export {users, setUser);

index.js

import {users, setUser} from './users.js'

console.log(users);
console.log(setUser(["a", "b", "c"]));
about 4 years ago · Juan Pablo Isaza Denunciar

0

As @pilchard explained, to accomplish my task I need to change this array inside the module. That's because values that I import are read-only outside the module. Docs.

users.js

let users = ["Jack", "Mary"];

function emptyUsers() {
    users = [];
}

export { users, emptyUsers };

index.js

import { users, emptyUsers } from "./users.js"
console.log(users); // ["Jack", "Mary"]

emptyUsers(); // instead of users = [];
console.log(users); // []

users.push('Ricky'); // Also I can still change the values of the imported array
console.log(users); // ['Ricky']
about 4 years ago · Juan Pablo Isaza Denunciar

0

try to import as different name:

import users as u from "./users.js"
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda