0

I am trying to access a specific directory in javascript. I tried to have access to it using require keyword as shown below

const path = require('../../var/opt/personal/guest/op/op_12201/data/persGuesOapDataFolder00/');

but when i run the code i get the following error:

Error: Cannot find module '../../var/opt/personal/guest/op/op_12201/data/persGuesOapDataFolder00/'

please let me know how import or to use a directory in javascript

Amrmsmb
  • 1
  • 27
  • 104
  • 226

3 Answers3

1

You can't possibly just import a directory unless there is index.js file within it. And in that index.js file, it should at least contain:

index.js:

import Foo from './Foo.js'
import Bar from './Bar.js'

export {
    Foo, 
    Bar
}

And then you can finally import it:

import {Foo, Bar} from '../../components';

Joshua Stephen
  • 196
  • 1
  • 8
0

When you point to a directory without specifying the file, the index.js file is imported. I do not think it's possible to import an entire directory. If you want to import all the functions of a directory, you can create an index.js and explicitly export them.

See: node.js require all files in a folder?

lmarqs
  • 1,469
  • 13
  • 16
0

you can do this in two different way

consider the following folder structure

-- app/
   |- asset/
      |- user.js
   |- main.js

here main.js you can import the user.js in this way import user from '../asset/user'

other way is installing dotenv using npm i dotenv --save and following way

require('dotenv').config();
import user from './asset/user
Roledenez
  • 751
  • 4
  • 16
  • 43