I have just worked through chapter 2 of the book Full Stack React Projects, Second Edition.
When I type into the Terminal yarn development
to check my code, I get this error message:
yarn run v1.22.15 warning package.json: License should be a valid SPDX license expression $ nodemon [nodemon] 2.0.2 [nodemon] to restart at any time, enter `rs` [nodemon] watching dir(s): server/**/* [nodemon] watching extensions: js,mjs,json [nodemon] starting `webpack --mode=development --config webpack.config.server.js && node ./dist/server.generated.js` Hash: 7630758f261bc6024be1 Version: webpack 4.42.1 Time: 475ms Built at: 10/06/2021 2:48:53 PM Asset Size Chunks Chunk Names server.generated.js 19.4 KiB main [emitted] main Entrypoint main = server.generated.js [0] multi ./server/server.js 28 bytes {main} [built] [./node_modules/webpack/buildin/harmony-module.js] (webpack)/buildin/harmony-module.js 573 bytes {main} [built] [./node_modules/webpack/buildin/module.js] (webpack)/buildin/module.js 497 bytes {main} [built] [./server/devBundle.js] 1.6 KiB {main} [built] [./server/server.js] 2.19 KiB {main} [built] [./template.js] 1.15 KiB {main} [built] [./webpack.config.client.js] 1.8 KiB {main} [built] [express] external "express" 42 bytes {main} [built] [mongodb] external "mongodb" 42 bytes {main} [built] [path] external "path" 42 bytes {main} [built] [webpack] external "webpack" 42 bytes {main} [built] [webpack-dev-middleware] external "webpack-dev-middleware" 42 bytes {main} [built] [webpack-hot-middleware] external "webpack-hot-middleware" 42 bytes {main} [built] webpack:///./server/server.js?:23 app.use('/dist', express__WEBPACK_IMPORTED_MODULE_0___default.a.static(path__WEBPACK_IMPORTED_MODULE_2___default.a.join(CURRENT_WORKING_DIR, 'dist'))); ^ ReferenceError: Cannot access 'app' before initialization at Module.eval (webpack:///./server/server.js?:23:1) at eval (webpack:///./server/server.js?:65:30) at Module../server/server.js (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:132:1) at __webpack_require__ (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:21:30) at eval (webpack:///multi_./server/server.js?:1:18) at Object.0 (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:166:1) at __webpack_require__ (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:21:30) at /Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:85:18 at Object.<anonymous> (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:88:10) at Module._compile (node:internal/modules/cjs/loader:1101:14) [nodemon] app crashed - waiting for file changes before starting...
I have tried the following solutions:
- https://stackoverflow.com/a/54450694/14537132
- https://stackoverflow.com/a/63298885/14537132
- Deleting nodemone.json and re-creating the file
- Deleting the node modules file & re-installing it
How do I fix this?
Advertisement
Answer
Deleting the node modules file won’t solve your issue since the problem according to the error log isn’t with any of the packages used in your app. Your app issue originates most likely from your server.js file as seen in your error log
ReferenceError: Cannot access 'app' before initialization at Module.eval (webpack:///./server/server.js?:23:1) at eval (webpack:///./server/server.js?:65:30) at Module../server/server.js (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:132:1) at __webpack_require__ (/Users/brittanysoozinsoozin/Library/Mobile Documents/com~apple~CloudDocs/Desktop/Desktop/TMDP/Molo9/Dev/Full Stack React/Ch2/MyCode/dist/server.generated.js:21:30) at eval (webpack:///multi_./server/server.js?:1:18)
what I can see is that the crash happens when nodejs tries to access the static files of your app which you declared in your server.js file on line 23.
You did not initialize the app variable before you used it on line 23. You would need to do something like this:
const app = require('./app'); // this will import your app.js file that is in your root folder assuming you are using that type of setup.
Also, check if the “dist” file path is correct.