SalesforceCommerceCloud / pwa-kit

React-based JavaScript frontend framework to create a progressive web app (PWA) storefront for Salesforce B2C Commerce.
https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/pwa-kit-overview.html
BSD 3-Clause "New" or "Revised" License
283 stars 130 forks source link

[BUG] Internal Server Error - starting local dev server fails (v2.3.0) #809

Closed tfoerster1211 closed 1 year ago

tfoerster1211 commented 1 year ago

Summary

When you create a new starter app project following the Quick Start instructions with v2.3.0, the startup of the local dev server fails with an error:

Uncaught exception:  TypeError: (0 , _crypto.randomUUID) is not a function
    at /Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/pwa-kit-dev/ssr/server/build-dev-server.js:105:53
    at Layer.handle [as handle_request] (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:328:13)
    at /Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:280:10)
[...]

Steps To Reproduce

Run

npx pwa-kit-create-app@latest --preset retail-react-app-demo

Start the local web server:

cd pwa-kit-starter-project
npm start

Expected result

The local dev server should start and build the app.

Actual result

In the browser you see the 'Internal Server Error'.

Stack trace (excerpt) from the terminal:

Uncaught exception:  TypeError: (0 , _crypto.randomUUID) is not a function
    at /Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/pwa-kit-dev/ssr/server/build-dev-server.js:105:53
    at Layer.handle [as handle_request] (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/layer.js:95:5)
    at trim_prefix (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:328:13)
    at /Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:286:9
    at Function.process_params (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:346:12)
    at next (/Users/tfoerster/Documents/Salesforce/pwa-kit-starter-project/node_modules/express/lib/router/index.js:280:10)
[...]

System Information (as applicable)

Browser: Chrome (or any other) Node version: 14.0.0 NPM version: 6.14.4 pwa-kit version: 2.3.0 Desktop OS: iOS

Additional information

The issue here is that function crypto.randomUUID() is not available with Node 14.0.0. It was added with Node 14.17.0/15.6.0 - see https://nodejs.org/api/crypto.html#cryptorandomuuidoptions

A local temporary solution/workaround is to replace the code line with res.locals.requestId = _crypto.randomBytes(20).toString('hex'); -> at least the dev server starts then. It might also work to use Node 14.17.0, but I didn't test that yet, and the documentation/package.json should be updated if this is a requirement.

tfoerster1211 commented 1 year ago

I just saw that in the latest docs node 14.19.0 is mentioned as a requirement. I think this was 14.0.0 before with v2.2.0.

https://developer.salesforce.com/docs/commerce/pwa-kit-managed-runtime/guide/setting-up-your-local-environment.html

Nevertheless, this is not a bug in this case.