keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Impossible to build/run a production build

Open JMLX42 opened this issue 3 years ago • 12 comments

How to reproduce

keystone build
keystone start

Expected behavior

The app is built into a production-ready output dir (ex : dist) and keystone start runs that build.

Actual behavior

keystone build does not appear to output any content that keystone dev does not.

$ keystone build
✨ Building Keystone
✨ Generating Admin UI code
✨ Generating Keystone config code
✨ Building Admin UI
info  - Skipping validation of types  
info  - Creating an optimized production build  
info  - Compiled successfully
info  - Collecting page data  
info  - Generating static pages (18/18)
info  - Finalizing page optimization  

Page                                       Size     First Load JS
┌ ○ /                                      1.74 kB         444 kB
├   /_app                                  0 B             442 kB
├ ○ /404                                   194 B           442 kB
├ λ /api/__keystone_api_build              0 B             442 kB
├ ○ /deliveries                            257 B           451 kB
├ ○ /deliveries/[id]                       3.72 kB         446 kB
├ ○ /environments                          257 B           451 kB
├ ○ /environments/[id]                     3.72 kB         446 kB
├ ○ /init                                  6.17 kB         448 kB
├ ○ /licenses                              253 B           451 kB
├ ○ /licenses/[id]                         3.72 kB         446 kB
├ ○ /no-access                             425 B           443 kB
├ ○ /organizations                         256 B           451 kB
├ ○ /organizations/[id]                    3.72 kB         446 kB
├ ○ /projects                              255 B           451 kB
├ ○ /projects/[id]                         3.72 kB         446 kB
├ ○ /signin                                1.6 kB          444 kB
├ ○ /users                                 251 B           451 kB
└ ○ /users/[id]                            3.72 kB         446 kB
+ First Load JS shared by all              442 kB
  ├ chunks/framework-b6621d06fb4f3aa0.js   42 kB
  ├ chunks/main-8f3915d3027d012d.js        26.9 kB
  ├ chunks/pages/_app-9ae8491b0b0789f0.js  373 kB
  └ chunks/webpack-5752944655d749a0.js     840 B

λ  (Server)  server-side renders at runtime (uses getInitialProps or getServerSideProps)
○  (Static)  automatically rendered as static HTML (uses no initial props)

Done in 17.24s.

Plus keystone start fails anyway:

✨ Starting Keystone
ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization
    at Object.Z (/app/.keystone/admin/.next/server/pages/api/__keystone_api_build.js:199:36)
    at Module.config (/app/.keystone/admin/.next/server/pages/api/__keystone_api_build.js:49:100)
    at Object.start (/app/node_modules/@keystone-6/core/scripts/dist/keystone-6-core-scripts.cjs.dev.js:464:65)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Environment

NodeJS version 16

  "dependencies": {
    "@keystone-6/auth": "^1.0.2",
    "@keystone-6/core": "^1.0.1",
    "@keystone-6/fields-document": "^1.0.1",
    "typescript": "^4.5.4",
}

JMLX42 avatar Mar 30 '22 13:03 JMLX42

The ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization error usually points at a circular dependency error:

  • https://stackoverflow.com/questions/65038253/uncaught-referenceerror-cannot-access-webpack-default-export-before-initi
  • https://stackoverflow.com/questions/64396536/vue-cannot-access-webpack-default-export-when-reference-a-components

Yet the exact same code runs just fine using keystone dev.

JMLX42 avatar Mar 30 '22 13:03 JMLX42

It does not happen after creating a fresh app with yarn create keystone-app though. So the problem might be in my code.

JMLX42 avatar Mar 30 '22 14:03 JMLX42

I have commented each schema one by one to see which one could cause a circular dependency. After finally gradually uncommenting each of them, I cannot reproduce the error and keystone start now works as expected:

smartshape-license-manager-app-1          | $ keystone start
smartshape-license-manager-app-1          | ✨ Starting Keystone
smartshape-license-manager-app-1          | ✨ Connecting to the database
smartshape-license-manager-app-1          | ✨ Creating server
smartshape-license-manager-app-1          | ✅ GraphQL API ready
smartshape-license-manager-app-1          | ✨ Preparing Admin UI Next.js app
smartshape-license-manager-app-1          | ✅ Admin UI ready
smartshape-license-manager-app-1          | ⭐️ Server Ready on http://localhost:3000

I'll keep this issue open until I can find a way to reproduce it.

JMLX42 avatar Mar 30 '22 14:03 JMLX42

The same error finally popped again after editing nothing but auth.js to set process.env.SESSION_SECURE == "true".

diff --git a/app/auth.ts b/app/auth.ts
index 48569a6..8733f8f 100644
--- a/app/auth.ts
+++ b/app/auth.ts
@@ -53,6 +53,7 @@ let sessionMaxAge = 60 * 60 * 24 * 30; // 30 days
 const session = statelessSessions({
   maxAge: sessionMaxAge,
   secret: sessionSecret!,
+  secure: process.env.SESSION_SECURE == "true",
 });
smartshape-license-manager-app-1          | $ keystone start
smartshape-license-manager-app-1          | ✨ Starting Keystone
smartshape-license-manager-app-1          | ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization
smartshape-license-manager-app-1          |     at Object.Z (/srv/app/.keystone/admin/.next/server/pages/api/__keystone_api_build.js:200:36)
smartshape-license-manager-app-1          |     at Module.config (/srv/app/.keystone/admin/.next/server/pages/api/__keystone_api_build.js:49:100)
smartshape-license-manager-app-1          |     at Object.start (/srv/app/node_modules/@keystone-6/core/scripts/dist/keystone-6-core-scripts.cjs.dev.js:464:65)
smartshape-license-manager-app-1          |     at processTicksAndRejections (node:internal/process/task_queues:96:5)
smartshape-license-manager-app-1          | error Command failed with exit code 1.
smartshape-license-manager-app-1          | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

So if there is any circular dependency issue, it is most likely not in my code.

I think the problem is trying to build/run a production build with NODE_ENV not actually set to "production".

JMLX42 avatar Mar 30 '22 15:03 JMLX42

I think the problem is trying to build/run a production build with NODE_ENV not actually set to "production".

Nope. That's not it.

JMLX42 avatar Mar 30 '22 16:03 JMLX42

(I deleted a duplicate comment) This is interesting to follow along @JMLX42! Please keep us updated, as soon as you have a reproduction we can try and action this.

dcousens avatar Mar 31 '22 00:03 dcousens

Hi guys I had similar issue (may be related). I was not able to run dev and also start commands without any errors printed to console. Screenshot 2022-04-01 at 10 34 34 Screenshot 2022-04-01 at 10 34 50

I was very confused because the day before everything worked and I made no changes.

After debugging I found that this was the issue Screenshot 2022-04-01 at 10 38 05

So I just closed the other application and everything started working.

It could help someone (maybe it would be better to print the error in console so everyone can understand what's happening behind the scenes)

mtrnik avatar Apr 01 '22 08:04 mtrnik

@mtrnik did you receive the error

✨ Starting Keystone
ReferenceError: Cannot access '__WEBPACK_DEFAULT_EXPORT__' before initialization

Or a listen EADDRINUSE: ... error?

dcousens avatar Apr 11 '22 06:04 dcousens

@dcousens no, I haven't received any error.

mtrnik avatar Apr 11 '22 06:04 mtrnik

@mtrnik I don't think your problem is related to this issue, please open a separate GitHub discussion, and we can help you there

dcousens avatar Apr 11 '22 06:04 dcousens

I was having a similar issue. I was using Render to deploy and that used node.js 17. I added:

"engines": { "node": "^14.15 || ^16.13" }

to the package.json and the application worked again.

junaid33 avatar May 01 '22 09:05 junaid33

@junaid33 the resolution you have posted indicates something different to the @JMLX42, who is using node@^16 according to the OP

dcousens avatar May 03 '22 03:05 dcousens

I just want to bump this open issue as there's an underlying point that I'd like to note relating to the @keystone/core files.

If you look at the dist director you'll notice webpack is outputting all 3 versions of every file: .dev.js, .prod.js, .cjs in the node modules

I don't actually believe there's any difference between the dev and prod files, but the production environment will still always run the .dev.js files instead of prod.

It seems that something is out of place in the webpack config that needs to be addressed here as a keystone build should only be referencing production enhanced files for keystone.

TheRealFlyingCoder avatar Nov 24 '22 05:11 TheRealFlyingCoder

I don't think this was reproducible

dcousens avatar Jul 11 '23 00:07 dcousens