error importing module

Asked

Viewed 69 times

0

I have this module in a file called timer.js

module.exports = {

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

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

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

only that you are returning to me this mistake:

Uncaught Error: Cannot find module './timer'

Can someone help me?

inserir a descrição da imagem aqui

  • send a tree of its main directory

  • Sorry, I don’t know what a Tree rs.

  • Its file structure, https://www.geeksforgeeks.org/tree-command-unixlinux/

  • I posted the image in the question

  • Your code is in the main.js file?

  • No, the file I want to export is in timer.js and I want to import it in index.js

Show 1 more comment

1 answer

1


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

Filing cabinet main.js

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

Filing cabinet js timer.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.