felixge / node-sandboxed-module

A sandboxed node.js module loader that lets you inject dependencies into your modules.
MIT License
342 stars 42 forks source link

Fix sandboxed-module not working when using npx #70

Open tie opened 4 years ago

tie commented 4 years ago

This PR fixes originPath being a URL instead of a path.

The requireLike(…).resolve function accepts a path, but CallSite.getFileName(…) returns a URI with file:// scheme. This commit fixes the issue by removing the prefix if origin path starts with file scheme.

Since module path resolution looks for node_modules in all directories of $PWD, the issue does not arise during development. However, scripts started by npx that depend on sandboxed-module package would break with MODULE_NOT_FOUND error.