Home > database >  File Importing From one Dir to another Dir
File Importing From one Dir to another Dir

Time:11-28

so basically my dir looks like this:

/node:

~/libs/lib.js

~/projects/main/script.js

and i want to import (the entire file) "lib" [\Node\libs\lib.js] into "script" [\Node\Projects\main\script.js], how do i do that?

thanks in advance, -Gzrespect

CodePudding user response:

Since your script.js is in the "main" folder of "Projects" folder, so you have to go 2 levels deeper. Add this codes into top part of your script.js.

  const connection = require("../../libs/lib");

CodePudding user response:

As I know you can't import the entire file from one directory to another but you can export the any Data Types primitive(int, string etc.) & non-primitive(objects & arrays).

const sum = (a,b) => return a   b;
const object = {}
const array = []

exports.module = {sum, object, array};

later in file in which you want to use that functions, objects and array..

const {sum, object, array} = require("file path"); 
  • Related