@@ -19,9 +19,15 @@ function getModuleNameFromPath(path: string) {
1919function resolvePathToModule ( moduleName : string , relativeTo : string ) {
2020 // if we require.resolve('my-module') then it will follow the main property in the linked package.json
2121 // but we want the root of the module so resolve to the package.json and then trim
22- return require
23- . resolve ( `${ moduleName } /package.json` , { paths : [ relativeTo ] } )
24- . slice ( 0 , - 12 ) ; // remove trailing `package.json`
22+ let resolved ;
23+ try {
24+ resolved = require
25+ . resolve ( `${ moduleName } /package.json` , { paths : [ relativeTo ] } ) ;
26+ }
27+ catch ( ex ) {
28+ return null ;
29+ }
30+ return resolved . slice ( 0 , - 12 ) ; // remove trailing `package.json`
2531}
2632
2733export function getDocumentContext ( documentUri : string , workspaceFolders : WorkspaceFolder [ ] ) : DocumentContext {
@@ -55,8 +61,10 @@ export function getDocumentContext(documentUri: string, workspaceFolders: Worksp
5561 if ( ref [ 0 ] === '~' && ref [ 1 ] !== '/' ) {
5662 const moduleName = getModuleNameFromPath ( ref . substring ( 1 ) ) ;
5763 const modulePath = resolvePathToModule ( moduleName , base ) ;
58- const pathWithinModule = ref . substring ( moduleName . length + 2 ) ;
59- return url . resolve ( modulePath , pathWithinModule ) ;
64+ if ( modulePath ) {
65+ const pathWithinModule = ref . substring ( moduleName . length + 2 ) ;
66+ return url . resolve ( modulePath , pathWithinModule ) ;
67+ }
6068 }
6169 return url . resolve ( base , ref ) ;
6270 } ,
0 commit comments