Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Calculator

0

77
Views
why the import/export from app.js to routes.js not working

I have a function in my app.js that I would like to use in my routes.js file, but the export is not working properly.

app.js:

module.exports = function extractquestions(){
     extractq();
};

routes.js:

let extractquestions = require('../app.js');

the error I get :

TypeError: extractquestions is not a function
7 months ago · Juan Pablo Isaza
2 answers
Answer question

0

The other guy is right. But you also do like this:

app.js:

function print(x){
    console.log(x);
}

module.exports = function printSenctence(sentence) {
    print(sentence);
}

and route.js:

const printSenctence = require('./app');
  
printSenctence("Everything is awesome!!");

Hope I help ;)

7 months ago · Juan Pablo Isaza Report

0

If you want to export function, try this:

helper.js

function add(a,b) {
  return a + b;
}

module.exports = {
  add
};

main.js

const { add } = require('./helper.js');
console.log(add(2,3));
7 months ago · Juan Pablo Isaza Report
Answer question
Find remote jobs