skip to Main Content

I’m trying to import my code functions from one file to another, however, it’s not working.

Look at the file I’m exporting from:

const uri = "mongodb+srv://<User>:<PassWord>@cluster0.ubhacr9.mongodb.net/?retryWrites=true&w=majority"



const client = new MongoClient(uri);


async function run(){
    //code
}

async function inserirDatabase(tipo, descricao, valor, id){
   //code
}

async function readDatabase (){
    //code
}

async function deleteOneOnDatabase(id){
   //code
}

module.exports = run, inserirDatabase, readDatabase, deleteOneOnDatabase 

Look how I’m importing the file:
import {run, inserirDatabase, readDatabase, deleteOneOnDatabase} from '../database/database.js';

2

Answers


  1. Instead of doing module.exports, try adding "export" before each function name. ie:

    export async function run(){
        //code
    }
    
    
    Login or Signup to reply.
  2. Export File:

    
    function somefunction1(){
     //codes
    }
    
    function somefunction2(){
     //codes
    }
    
    module.exports = {
     somefunction1: somefunction1,
     somefunction2: somefunction2
    }
    

    Import File:

    const {somefunction1, somefunction2} = require("path/to/file.js")
    somefunction1()
    somefunction2()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search