Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Cannot read property 'startsWith' of undefined #79

Closed
UnightSun opened this issue Oct 12, 2018 · 2 comments · Fixed by #80 or #82
Closed

Cannot read property 'startsWith' of undefined #79

UnightSun opened this issue Oct 12, 2018 · 2 comments · Fixed by #80 or #82

Comments

@UnightSun
Copy link

UnightSun commented Oct 12, 2018

friendly-errors-webpack-plugin: "version": "1.7.0"
webpack: "version": "4.20.2"

when code require a non-exists path like:
require.context('./components', false, /[\w-]+\.vue$/);

webpack will throw an exception:

ModuleNotFoundError: Module not found: Error: Can't resolve './components' in '/Users/unightsun/workspace/js/crawler-v2/cms-server/src' at factory.create (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/webpack/lib/Compilation.js:798:10) at asyncLib.parallel (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/webpack/lib/ContextModuleFactory.js:143:23) at /Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/neo-async/async.js:2817:7 at /Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/neo-async/async.js:6783:13 at contextResolver.resolve (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/webpack/lib/ContextModuleFactory.js:118:26) at doResolve (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/Resolver.js:184:12) at hook.callAsync (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/Resolver.js:238:5) at _fn0 (eval at create (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1) at resolver.doResolve (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/UnsafeCachePlugin.js:37:5) at hook.callAsync (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/Resolver.js:238:5) at _fn0 (eval at create (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:15:1) at hook.callAsync (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/Resolver.js:238:5) at _fn0 (eval at create (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:12:1) at resolver.doResolve (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js:42:38) at hook.callAsync (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/enhanced-resolve/lib/Resolver.js:238:5) at _fn3 (eval at create (/Users/unightsun/workspace/js/crawler-v2/cms-server/node_modules/tapable/lib/HookCodeFactory.js:32:10), <anonymous>:42:1) resolve './components' in '/Users/unightsun/workspace/js/crawler-v2/cms-server/src' using description file: /Users/unightsun/workspace/js/crawler-v2/cms-server/package.json (relative path: ./src) Field 'browser' doesn't contain a valid alias configuration using description file: /Users/unightsun/workspace/js/crawler-v2/cms-server/package.json (relative path: ./src/components) as directory /Users/unightsun/workspace/js/crawler-v2/cms-server/src/components doesn't exist

it's dependencies is:
[ RequireContextDependency { module: null, weak: false, optional: false, loc: SourceLocation { start: [Object], end: [Object] }, options: { request: './components', recursive: false, regExp: /[\w-]+\.vue$/, mode: 'sync' }, userRequest: './components', critical: false, hadGlobalOrStickyRegExp: false, range: [ 384, 438 ] } ]

so 'friendly-errors-webpack-plugin/src/transformers/moduleNotFound.js:17':

const module = webpackError.dependencies[0].request;

will be undefined.

and then 'friendly-errors-webpack-plugin/src/formatters/moduleNotFound.js:5'

return module.startsWith('./') || module.startsWith('../');

will throw a TypeError: Cannot read property 'startsWith' of undefined

optional fix will be 'friendly-errors-webpack-plugin/src/transformers/moduleNotFound.js:17':

- const module = webpackError.dependencies[0].request;
+ const dependency = webpackError.dependencies[0];
+ const module = dependency.request || dependency.options.request;

UnightSun pushed a commit to UnightSun/friendly-errors-webpack-plugin that referenced this issue Oct 12, 2018
@UnightSun UnightSun mentioned this issue Oct 12, 2018
@UnightSun
Copy link
Author

Tried to create a pull request to fix this.

@ycjcl868
Copy link

+1

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants