Skip to content

Commit

Permalink
Fix cannot find module (#6554)
Browse files Browse the repository at this point in the history
* Add test and fixture

* Fix cannot find module issue

createRequire expects a path to a filename (or URL). When using cwd() it
assumes the last directory is a filename and the require will be based
upon the wrong directory (1 level up).
  • Loading branch information
leahciMic authored Aug 24, 2021
1 parent cd0195a commit 35199de
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/tasty-steaks-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/plugin-helpers': patch
---

Fix module not found bug in resolveExternalModuleAndFn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export function resolveExternalModuleAndFn(pointer: any): any {
if (moduleName === 'change-case-all') {
loadedModule = changeCaseAll;
} else {
const cwdRequire = createRequire(cwd());
// we have to use a path to a filename here (it does not need to exist.)
// https://github.com/dotansimha/graphql-code-generator/issues/6553
const cwdRequire = createRequire(cwd() + '/index.js');
loadedModule = cwdRequire(moduleName);

if (!(functionName in loadedModule) && typeof loadedModule !== 'function') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.test = 'foobar';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import path from 'path';
import { resolveExternalModuleAndFn } from '../src/resolve-external-module-and-fn';

describe.only('resolveExternalModuleAndFn', () => {
describe('Issues', () => {
it('#6553 - Cannot find module', () => {
const relativePathToSelf = path.relative(process.cwd(), path.join(__dirname, './fixtures/externalModuleFn.js'));
expect(resolveExternalModuleAndFn('./' + relativePathToSelf + '#test')).toBe('foobar');
});
});
});

0 comments on commit 35199de

Please sign in to comment.