Search code examples
javascriptarraysmodulewebpackrequire

Webpack cannot resolve object require


I'm building a webapp and i need webpack to require an object in a config file up one folder.

└─ config.js
└─ foo
   └─bar.js

let's say that bar.js needs to get a kitty const in config.js

At the moment my code look like this in config.js javascript

const kitty = {
    meow: {
        'name': "prrr",
    }
};

and like this in bar.js javascript

require('../config')
console.log(kitty.meow.name)

but I'm returning an error that says

Module not found: Error: Cannot resolve 'file' or 'directory' ../config

Solution

  • I solved this using es6 syntax

    export const kitty = {...}
    

    and

    import kitty from '../config.js'