Skip to content
This repository was archived by the owner on Feb 26, 2026. It is now read-only.

Commit 93676e3

Browse files
authored
Merge pull request #21 from apostrophecms/pro-2080
PRO-2080 if there is no package.json at all in the project folder, allow recursive lookup
2 parents 5126837 + 9b7369f commit 93676e3

5 files changed

Lines changed: 828 additions & 5 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ If the same module exists in two places, an exception is thrown.
350350

351351
**The 2.x series is deprecated for new work, as its functionality was folded into Apostrophe 3.x. See below for 1.x release notes relevant to maintenance of Apostrophe 2.x.**
352352

353+
1.3.2: starting in version 1.3.1, this module only loads other modules via `npm` if they are explicit npm dependencies, which is necessary for stability and security. However, it is too strict: if the project has no `package.json` at all at the level of `app.js`, `npm` search up the tree, and this module should too. Beginning in verison 1.3.2, it does search up the tree. However it stops at the first `package.json` found.
354+
353355
1.3.1: `moog-require` loads modules from npm if they exist there and are configured by name in the application. This was always intended only as a way to load direct, intentional dependencies of your project. However, since npm "flattens" the dependency tree, dependencies of dependencies that happen to have the same name as a project-level module could be loaded by default, crashing the site or causing unexpected behavior. So beginning with this release, `moog-require` scans `package.json` to verify an npm module is actually a dependency of the project itself before attempting to load it.
354356

355357
1.3.0: achieved an approximately 100x performance improvement when `nestedModuleSubdirs` is in use by fetching

index.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,26 @@ module.exports = function(options) {
162162
// Even if the package exists in node_modules it might just be a
163163
// sub-dependency due to npm/yarn flattening, which means we could be
164164
// confused by an unrelated npm module with the same name as an Apostrophe
165-
// module unless we verify it is a real project-level dependency
165+
// module unless we verify it is a real project-level dependency. However
166+
// if no package.json at all exists at project level we do search up the
167+
// tree until we find one to accommodate patterns like `src/app.js`
166168
if (!self.validPackages) {
167-
const info = JSON.parse(fs.readFileSync(`${path.dirname(self.root.filename)}/package.json`, 'utf8'));
168-
self.validPackages = new Set([ ...Object.keys(info.dependencies || {}), ...Object.keys(info.devDependencies || {}) ]);
169+
let info = null;
170+
const initialFolder = path.dirname(self.root.filename);
171+
let folder = initialFolder;
172+
while (true) {
173+
const file = `${folder}/package.json`;
174+
if (fs.existsSync(file)) {
175+
const info = JSON.parse(fs.readFileSync(file, 'utf8'));
176+
self.validPackages = new Set([ ...Object.keys(info.dependencies || {}), ...Object.keys(info.devDependencies || {}) ]);
177+
break;
178+
} else {
179+
folder = path.dirname(folder);
180+
if (!folder.length) {
181+
throw new Error(`package.json was not found in ${initialFolder} or any of its parent folders.`);
182+
}
183+
}
184+
}
169185
}
170186
if (!self.validPackages.has(type)) {
171187
return null;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"url": "git://github.com/apostrophecms/moog-require.git"
3737
},
3838
"scripts": {
39-
"test": "mocha test/test.js"
39+
"test": "mocha test/test.js test/nested/test.js"
4040
},
41-
"version": "1.3.1"
41+
"version": "1.3.2"
4242
}

test/nested/project_modules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../project_modules/

0 commit comments

Comments
 (0)