package.json. It happens because Node.js resolves modules by walking up the directory tree — a package installed by a dependency of yours is accessible to your code, even though you never declared it.
In mini-program development this is particularly risky: the package might disappear or change version when the dependency that brought it in is upgraded, causing hard-to-debug runtime failures.
MorJS’s phantomDependency option analyzes imports and require() calls in all source files (excluding node_modules) and compares them against your package.json dependencies.
Enabling the check
SetphantomDependency to an object in your compile configuration:
false (the default) to disable the check entirely:
Configuration options
mode
mode
Controls what happens when phantom dependencies are detected.
| Value | Behavior |
|---|---|
'warn' | Prints a warning table listing each phantom dependency and the source file that imports it. Compilation continues. |
'error' | Prints an error table and throws, causing the process to exit with code 1. In watch mode, the error is logged but the process does not exit. |
exclude
exclude
An array of npm package names to skip, even when they are not listed in
package.json.Common use cases:- Packages provided by the mini-program runtime environment.
- Peer dependencies expected to be installed by consumers.
- Packages listed in a workspace root
package.jsonrather than the local one.
What is checked
ThePhantomDependencyPlugin (in packages/plugin-compiler/src/plugins/phantomDependencyPlugin.ts) instruments the TypeScript compiler transformer pipeline to collect every import and require() call found in:
- Script files (
.js,.ts,.mjs,.mts) - SJS files (
.sjs,.wxs)
., /, @/, or @@/) are ignored. Only bare package specifiers are checked.
At the end of compilation (done hook), the collected specifiers are compared against:
dependenciesanddevDependenciesinpackage.jsonat the project root.dependenciesanddevDependenciesinpackage.jsonfiles found inside eachsrcPathsentry.- Keys in
aliasandresolve.alias(aliased paths are not real packages). - Packages listed in
externalsorconsumes. - The
excludearray fromphantomDependencyconfig.
Example output
When phantom dependencies are detected inwarn mode, MorJS logs:
error mode the same table is printed at error level and the build exits with code 1.
Resolving phantom dependencies
Identify the packages
Run
mor compile (or mor compile --watch) and look for the phantom dependency table in the output.