ilearnio / module-alias

Register aliases of directories and custom module paths in Node
MIT License
1.76k stars 69 forks source link

[bugfix] forbid using __webpack_require__ search for package.json #61

Closed adispring closed 5 years ago

adispring commented 5 years ago

When I use webpack to compile on node, require will be replaced by __webpack_require__, but __webpack_require__ can not support dynamic require modules, which will result in searching package.json failed.

Discussion about dynamic require on node with webpack: https://github.com/webpack/webpack/issues/4175

current webpack output file:

try{s=t[o],n=i(369)(r.join(s,"package.json"));break}catch(e){}

forbid using __webpack_require__:

try{n=t[s],i=require(r.join(n,"package.json"));break}catch(e){}
Kehrlann commented 5 years ago

Hi ! Thanks for contributing !

I'm not sure we should merge this in, though ... 😐

  1. Webpack has its own module resolution, so you shouldn't be using module-alias https://webpack.js.org/concepts/module-resolution/
  2. Webpack is for packaging web apps ; but module-alias is specifically built for server-side node projects. So you really shouldn't be using it in a project that uses webpack.