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

114
Views
Did node require change?

I previously included other js files into my node projects using require as seen on this post. But for some reason this no longer works, did Node change or am I missing some mistake?

This is my code:

main.js:

require("./test");

console.log(x);

test.js:

var x = 3;

Running this code results in this error message:

main.js:3
console.log(x);
            ^

ReferenceError: x is not defined
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can't use variable declare in the required file without exporting variables.

More document about export

test.js:

var x = 3;

module.exports.x = x;

main.js:

var test = require("./test");

console.log(test.x);
about 4 years ago · Santiago Trujillo Report

0

Well, you need to add this - test.js:

const x = 3;
module.exports = x;

main.js:

const x = require('./test.js');
console.log(x);

And documentation: https://nodejs.org/api/modules.html

about 4 years ago · Santiago Trujillo Report

0

you need to export var from test.js

export var x = 3;

after that import into main.js

var { x } = require("./test");
console.log(x);
about 4 years ago · Santiago Trujillo 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!