Skip to content

Commit b3f4aa1

Browse files
authored
Use require.resolve to load fastify (#94)
1 parent bfaedfa commit b3f4aa1

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const semver = require('semver')
44
const console = require('console')
55
const extractPluginName = require('./stackParser')
6+
const { join, dirname } = require('path')
67

78
function plugin (fn, options = {}) {
89
if (typeof fn.default !== 'undefined') { // Support for 'export default' behaviour in transpiled ECMAScript module
@@ -58,7 +59,8 @@ function checkVersion (version, pluginName) {
5859

5960
var fastifyVersion
6061
try {
61-
fastifyVersion = semver.coerce(require('fastify/package.json').version)
62+
const pkgPath = join(dirname(require.resolve('fastify', { paths: [require.main.filename] })), 'package.json')
63+
fastifyVersion = semver.coerce(require(pkgPath).version)
6264
} catch (_) {
6365
console.info('fastify not found, proceeding anyway')
6466
}

test/test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const proxyquire = require('proxyquire')
55
const test = t.test
66
const fp = require('../plugin')
77
const Fastify = require('fastify')
8+
const { join, normalize } = require('path')
89

910
test('fastify-plugin is a function', t => {
1011
t.plan(1)
@@ -127,7 +128,7 @@ test('should not throw if fastify is not found', t => {
127128
t.plan(1)
128129

129130
const fp = proxyquire('./../plugin.js', {
130-
'fastify/package.json': null,
131+
[normalize(join(__dirname, '..', 'node_modules', 'fastify', 'package.json'))]: null,
131132
console: {
132133
info: function (msg) {
133134
t.is(msg, 'fastify not found, proceeding anyway')

0 commit comments

Comments
 (0)