Skip to content

Commit

Permalink
Handle there being no parent module (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
JulienKode and sindresorhus committed Nov 1, 2020
1 parent 8a7a217 commit 3d27ebc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion index.js
Expand Up @@ -10,7 +10,8 @@ module.exports = moduleId => {

const parentPath = parentModule(__filename);

const filePath = resolveFrom(path.dirname(parentPath), moduleId);
const cwd = parentPath ? path.dirname(parentPath) : __dirname;
const filePath = resolveFrom(cwd, moduleId);

const oldModule = require.cache[filePath];
// Delete itself from module parent
Expand Down
21 changes: 21 additions & 0 deletions test.js
Expand Up @@ -38,3 +38,24 @@ test('import when parent removed from cache', t => {
importer(id);
});
});

test('should not fail when there is no parent module', t => {
const targetPath = require.resolve('parent-module');
const orignalModule = require.cache[targetPath];
delete require.cache[targetPath];
delete require.cache[require.resolve('.')];
require.cache[targetPath] = {
loaded: true,
id: targetPath,
exports: () => undefined
};

const id = './fixture-importer';
const importer = importFresh(id);

t.notThrows(() => {
importer(id);
});

require.cache[targetPath] = orignalModule;
});

0 comments on commit 3d27ebc

Please sign in to comment.