Skip to content

Commit

Permalink
Handle modules that cannot be loaded in node
Browse files Browse the repository at this point in the history
If there is some error when requiering a module that is not that the
module does not exist, we know that we have found the right path. It is
therefore returned with a fake module instance.

This should be considered experimental but will unlock some blocked use
cases.
  • Loading branch information
TheLudd committed Jan 23, 2018
1 parent cc4457e commit e4f25a9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
13 changes: 9 additions & 4 deletions lib/find-module.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ tryFind = (base, fileName, callers) ->
instance: require fileName
path: fileName
catch e
try
findModule(base, fileName, callers)
catch e2
throw new CouldNotLoad base, fileName, callers
if e.code == 'MODULE_NOT_FOUND'
try
findModule(base, fileName, callers)
catch e2
throw new CouldNotLoad base, fileName, callers
else
instance: {}
path: makePath base, fileName, callers


exports.instance = (base, fileName, callers) ->
tryFind(base, fileName, callers).instance
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"coffee-script": "^1.9.0",
"mocha": "^5.0.0",
"mocha-gwt": "^0.2.0",
"pikaday": "^1.7.0",
"ramda": "^0.25.0",
"source-map-support": "^0.5.2"
},
Expand Down
13 changes: 11 additions & 2 deletions test/e2e/find-module-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ describe 'findModule', ->

verifyResult = ->
(@base + '/' + @expectedPath).should.equal @resultPath
expectedInstance = req @expectedPath
expectedInstance.should.deep.equal @resultInstance
unless @expectedInstance?
@expectedInstance = req @expectedPath
@expectedInstance.should.deep.equal @resultInstance

afterBlock ->
if @error?
Expand Down Expand Up @@ -72,3 +73,11 @@ describe 'findModule', ->
@fileName = 'nonExisting'
@callers = [ 'fake2', 'fake3' ]
Then -> @error?

describe 'troublesome module', ->
Given ->
@expectedInstance = {}
@expectedPath = 'node_modules/pikaday'
@fileName = 'pikaday'
@callers = []
Then verifyResult

0 comments on commit e4f25a9

Please sign in to comment.