Can't ES6 import with root from specs folder


When using import bar from './lib/foo' in a test file in the package specs folder I’m getting the following error when running package specs:

Error: Cannot find module './lib/foo'

While this does work in a file in the lib folder (file does exists)! The workaround is to use '../lib/foo' (notice the two dots).

My question is why this doesn’t work in the specs folder… bug or another reason?


The JS in the specs folder is being run in the context of the specs folder (because that’s where it’s being run from). Whereas, otherwise I assume your normal Electron entry point is a main.js that lies at the same level as your lib folder, specified in your package.json.

So while ./lib/foo refers to what you expect in your app, when running tests, it instead refers to specs/lib/foo, which I assume doesn’t exist. Whereas, the .. is going up a level, out of the specs folder, pointing to the file you actually expect.


Of course, both ./ and __dirname used with require() translate to the directory of the current file.