Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
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 answers
Answer question

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 Report

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 Report

0

try to import as different name:

import users as u from "./users.js"
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!