Handle broken NODE_PATH setups gracefully (#50)
Previously, when NODE_PATH had a trailing separator like `/foo:/bar:`,
resolving a module would hang indefinitely when the module was not found
diff --git a/index.js b/index.js
index 769ccc8..6899a7e 100644
--- a/index.js
+++ b/index.js
@@ -125,8 +125,9 @@
// from `basedir`
function resolvePathToPackage (name, basedir) {
return Promise.resolve().then(_ => {
- const { NODE_PATH } = process.env;
- const paths = NODE_PATH ? NODE_PATH.split(path.delimiter) : [];
+ const paths = (process.env.NODE_PATH || '')
+ .split(path.delimiter)
+ .filter(p => p);
// We resolve the path to the module's package.json to avoid getting the
// path to `main` which could be located anywhere in the package
diff --git a/spec/fetch-unit.spec.js b/spec/fetch-unit.spec.js
index 9cbe08b..e4c612f 100644
--- a/spec/fetch-unit.spec.js
+++ b/spec/fetch-unit.spec.js
@@ -79,16 +79,22 @@
});
describe('resolvePathToPackage', () => {
- let tmpDir, resolvePathToPackage, expectedPath;
+ let tmpDir, resolvePathToPackage, expectedPath, NODE_PATH;
beforeEach(() => {
tmpDir = getTmpDir();
resolvePathToPackage = rewire('..').__get__('resolvePathToPackage');
expectedPath = path.join(tmpDir, 'app/node_modules/dummy-local-plugin');
fs.copySync(path.join(__dirname, 'support/dummy-local-plugin'), expectedPath);
+
+ NODE_PATH = process.env.NODE_PATH;
+ delete process.env.NODE_PATH;
});
afterEach(() => {
+ if (NODE_PATH !== undefined) {
+ process.env.NODE_PATH = NODE_PATH;
+ }
fs.removeSync(tmpDir);
});
@@ -129,4 +135,12 @@
process.env.NODE_PATH = path.join(tmpDir, 'app/node_modules');
return resolveToExpectedPathFrom(path.join(tmpDir, 'another-app'));
});
+
+ it('should gracefully handle broken $NODE_PATH', () => {
+ process.env.NODE_PATH = path.delimiter;
+ return resolvePathToPackage('dummy-local-plugin', tmpDir).then(
+ _ => fail('expect promise to be rejected'),
+ err => expect(err).toBeDefined()
+ );
+ });
});