error importing module

0

I have this module in a file called timer.js

module.exports = {

    iniciar(){
        console.log('oi');
    }
}

I'm trying to import this module into the index.js file as follows:

const timer = require('./timer');

Only this error is returning me:

Uncaught Error: Cannot find module './timer'

Can anyone help me?

    
asked by anonymous 17.09.2018 / 14:36

1 answer

1

Hello, are you sure that time.js is in the same directory ?? Because I did not find any errors, and also can not forget that when a function is called before the name of the function, the name of the variable that was assigned require. I made two ways to create functions and put them in exports, See this example:

File main.js

const timer = require('./timer');
timer.iniciar1();
timer.iniciar2();

Archive timer.js

const iniciar1 = () =>{
    console.log("oi 1");
};

module.exports = {
    iniciar1,
    iniciar2(){
        console.log("oi 2");
    },
};
    
17.09.2018 / 15:15