Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to run with webpack 5: TypeError: Cannot read property 'startsWith' of undefined #371

Closed
knightus opened this issue Mar 13, 2021 · 1 comment

Comments

@knightus
Copy link

Hi! 👋

Firstly, thanks for your work on this project! 🙂

I was trying to upgrade my stack from webpack 4 to webpack 5. And got the following error from dotenv-webpack:

[webpack-cli] TypeError: Cannot read property 'startsWith' of undefined
    at Dotenv.shouldStub (F:\_projects_\...\node_modules\dotenv-webpack\dist\index.js:210:20)
    at Dotenv.formatData (F:\_projects_\...\node_modules\dotenv-webpack\dist\index.js:192:16)
    at Dotenv.apply (F:\_projects_\...\node_modules\dotenv-webpack\dist\index.js:66:23)
    at WrappedPlugin.apply (F:\_projects_\...\node_modules\speed-measure-webpack-plugin\WrappedPlugin\index.js:292:29)
    at createCompiler (F:\_projects_\...\node_modules\webpack\lib\webpack.js:71:12)
    at create (F:\_projects_\...\node_modules\webpack\lib\webpack.js:118:16)
    at webpack (F:\_projects_\...\node_modules\webpack\lib\webpack.js:126:47)
    at WebpackCLI.f [as webpack] (F:\_projects_\...\node_modules\webpack\lib\index.js:41:15)
    at WebpackCLI.createCompiler (F:\_projects_\...\node_modules\webpack-cli\lib\webpack-cli.js:1678:29)
    at async WebpackCLI.buildCommand (F:\_projects_\...\node_modules\webpack-cli\lib\webpack-cli.js:1785:20)
    at async Command.<anonymous> (F:\_projects_\...\node_modules\webpack-cli\lib\webpack-cli.js:564:25)
    at async Promise.all (index 1)
    at async Command.<anonymous> (F:\_projects_\...\node_modules\webpack-cli\lib\webpack-cli.js:1120:13)
error Command failed with exit code 2.

Further debugging, I've found that compiler.webpack.version is undefined.

I've used patch-package to patch dotenv-webpack@7.0.1 to make it work. Here is the diff that solved my problem:

diff --git a/node_modules/dotenv-webpack/dist/index.js b/node_modules/dotenv-webpack/dist/index.js
index cf85f6c..f1d8b8b 100644
--- a/node_modules/dotenv-webpack/dist/index.js
+++ b/node_modules/dotenv-webpack/dist/index.js
@@ -62,7 +62,7 @@ var Dotenv = /*#__PURE__*/function () {
 
       var variables = this.gatherVariables();
       var target = (_compiler$options$tar = compiler.options.target) !== null && _compiler$options$tar !== void 0 ? _compiler$options$tar : 'web';
-      var version = compiler.webpack.version;
+      var version = compiler.webpack.version ?? '5';
       var data = this.formatData({
         variables: variables,
         target: target,

This issue body was partially generated by patch-package.

@knightus
Copy link
Author

It seems to be an issue with speed-measure-webpack-plugin. I will close this issue for now. Sorry for bother

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant