question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Webpack5: `start-storybook` fails when `.env` is present

See original GitHub issue

Describe the bug This was a doozy to track down! 😅 The gist is, you get a failure with a strange error if you have a .env file under the right npm conditions. This happens because @storybook/builder-webpack4 and @storybook/core-server depend on dotenv-webpack 1.8, which does not support Webpack 5.

If npm hoists dotenv-webpack 6.0.4 to the node_modules root, everything works fine. However, if npm hoists dotenv-webpack 1.8, and the user has a .env file present, start-storybook fails to run.

Unless the user is already pinning dotenv-webpack, the version that gets hoisted depends on how many dependencies are using 1.8 vs 6.0.4. (For example, in the repro steps below, if you remove @storybook/addon-essentials, npm hoists 6.0.4 and things work normally.) This makes for some extremely surprising and hard-to-pin-down behavior.

I believe the proper remedy is to update all Storybook libraries that depend on dotenv-webpack to v6, which supports Webpack 4 and 5. I am happy to open a PR if desired.

For context, here is the error when the wrong version of dotenv-webpack is hoisted:

TypeError: Cannot read property 'get' of undefined
    at /Users/paul/Development/balsa/storybook-repro/node_modules/webpack/lib/DefinePlugin.js:549:57
    at Array.forEach (<anonymous>)
    at walkDefinitionsForValues (/Users/paul/Development/balsa/storybook-repro/node_modules/webpack/lib/DefinePlugin.js:545:31)
    at /Users/paul/Development/balsa/storybook-repro/node_modules/webpack/lib/DefinePlugin.js:571:5
    at SyncHook.eval [as call] (eval at create (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/HookCodeFactory.js:19:10), <anonymous>:7:1)
    at SyncHook.lazyCompileHook (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.newCompilation (/Users/paul/Development/balsa/storybook-repro/node_modules/@storybook/core-server/node_modules/webpack/lib/Compiler.js:631:26)
    at /Users/paul/Development/balsa/storybook-repro/node_modules/@storybook/core-server/node_modules/webpack/lib/Compiler.js:667:29
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
    at AsyncSeriesHook.lazyCompileHook (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/Hook.js:154:20)
    at Compiler.compile (/Users/paul/Development/balsa/storybook-repro/node_modules/@storybook/core-server/node_modules/webpack/lib/Compiler.js:662:28)
    at /Users/paul/Development/balsa/storybook-repro/node_modules/@storybook/core-server/node_modules/webpack/lib/Watching.js:77:18
    at _next0 (eval at create (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:25:1)
    at eval (eval at create (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:34:1)
    at watchRunHook (/Users/paul/Development/balsa/storybook-repro/node_modules/webpack-virtual-modules/index.js:173:5)
    at AsyncSeriesHook.eval [as callAsync] (eval at create (/Users/paul/Development/balsa/storybook-repro/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:30:1)

To Reproduce Steps to reproduce the behavior:

# From an empty directory
npm init -y
npm install react{,-dom}@16
npm install --save-dev webpack
npm install --save-dev --legacy-peer-deps @storybook/{addon-essentials,builder-webpack5,react}
mkdir .storybook
echo "module.exports = { core: { builder: 'webpack5' } };" > .storybook/main.js
echo "FOO=bar" > .env
npx start-storybook

Expected behavior Storybook should start.

System

System:
    OS: macOS 11.2.3
    CPU: (8) x64 Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
  Binaries:
    Node: 15.12.0 - /usr/local/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 7.7.6 - /usr/local/bin/npm
  Browsers:
    Chrome: 89.0.4389.90
    Firefox: 86.0
    Safari: 14.0.3
  npmPackages:
    @storybook/addon-essentials: ^6.2.0 => 6.2.0
    @storybook/builder-webpack5: ^6.2.0 => 6.2.0
    @storybook/react: ^6.2.0 => 6.2.0

┆Issue is synchronized with this Asana task by Unito

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
michael-woodward-spokcommented, Apr 6, 2021

I think it’s worth trying @paulrosania’s suggestion:

I believe the proper remedy is to update all Storybook libraries that depend on dotenv-webpack to v6, which supports Webpack 4 and 5. I am happy to open a PR if desired.

4reactions
michael-woodward-spokcommented, Apr 5, 2021

The workaround is to add dotenv-webpack@6.0.4 to your devDependencies list

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to inject storybook with Environment Variables
I'm using windows system,i have to input 'set STORYBOOK_THEME=red 'to set the Environment Variables,and then i can get this var by 'process.env.
Read more >
Environment variables - Storybook - JS.ORG
If you supply an environment variable prefixed with STORYBOOK_ , it will be available in process.env when using webpack, or import.meta.env when using...
Read more >
Storybook with builder-webpac5 complains about conflicting ...
Start storybook with yarn start-storybook. Expected behavior. No error in build console: Conflicting values for 'process.env' '{NODE_ENV: "development", ...
Read more >
How to use .env file in JavaScript applications with webpack
Somehow, Dotenv parses the variables before Webpack, then, when webpack is going to parse, it retrieves an error, because he uses the string ......
Read more >
A Dead Simple Guide to Using Environment Variables in Your ...
process.env.MY_SECRET_KEY // error: process is not defined. Finding dotenv-webpack was the easy solution I was looking for but I found its instructions were ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found