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

Cannot read properties of undefined (reading 'cacheTag') #767

Open
FilipRybinski opened this issue Feb 18, 2025 · 3 comments
Open

Cannot read properties of undefined (reading 'cacheTag') #767

FilipRybinski opened this issue Feb 18, 2025 · 3 comments
Labels
bug Something isn't working

Comments

@FilipRybinski
Copy link

For which library do you need help?

native-federation

Question

Any advice on how to resolve this issue? It appears when I try to start the Node server (server.mjs).
package.json
{
"name": "microfrontends",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"serve:ssr:host": "node dist/host/server/server.mjs"
},
"private": true,
"dependencies": {
"@angular/animations": "^19.0.0",
"@angular/common": "^19.0.0",
"@angular/compiler": "^19.0.0",
"@angular/core": "^19.0.0",
"@angular/forms": "^19.0.0",
"@angular/platform-browser": "^19.0.0",
"@angular/platform-browser-dynamic": "^19.0.0",
"@angular/platform-server": "^19.0.0",
"@angular/router": "^19.0.0",
"@angular/ssr": "^19.1.7",
"@softarc/native-federation-node": "^2.0.10",
"cors": "^2.8.5",
"es-module-shims": "^1.5.12",
"express": "^4.18.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.15.0"
},
"devDependencies": {
"@angular-architects/native-federation": "^18.2.3",
"@angular-devkit/build-angular": "^19.1.7",
"@angular/cli": "^19.0.4",
"@angular/compiler-cli": "^19.0.0",
"@types/express": "^4.17.17",
"@types/jasmine": "~5.1.0",
"@types/node": "^18.18.0",
"jasmine-core": "~5.4.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.6.2"
}
}

@manfredsteyer
Copy link
Contributor

Oh, this looks like a bug. I will take care of it asap.

@manfredsteyer manfredsteyer added the bug Something isn't working label Feb 19, 2025
@FilipRybinski
Copy link
Author

Awesome, I'm waiting for updates because I was struggling with the SSR configuration. Thanks :)

@cristiannores
Copy link

I encountered the same issue with the server.mjs file generated during the build process of an Angular application utilizing Native Federation. Specifically, the problematic line:

e?.cacheTag ? `?t=${e.cacheTag}` : ""

resulted in a runtime error because e was undefined. To resolve this, I created a postbuild.js script that modifies the generated server.mjs file after the build process. Here's the content of postbuild.js:

const fs = require('fs');
const path = require('path');

const serverFilePath = path.join(__dirname, 'dist', 'mc-auth-app', 'server', 'server.mjs');

fs.readFile(serverFilePath, 'utf8', (err, data) => {
  if (err) {
    console.error('Error reading server.mjs:', err);
    return;
  }

  // Replace the problematic line
  const result = data.replace(/e\?\.(cacheTag\s?\?\s?`[^`]+`\s?:\s?"";?)/g, '(e && e.cacheTag ? `?t=${e.cacheTag}` : "")');

  fs.writeFile(serverFilePath, result, 'utf8', (err) => {
    if (err) console.error('Error writing server.mjs:', err);
    else console.log('server.mjs modified successfully.');
  });
});

In this script, replace 'mc-auth-app' with the actual name of your application.

Additionally, I observed that to run the server correctly, it was necessary to navigate to the dist/mc-auth-app/server directory and execute node server.mjs from there, due to the relative path ../browser used within the server code.

To automate these adjustments and streamline the build and serve process, I incorporated the following scripts into my package.json:

{
  "scripts": {
    "build": "ng build && node postbuild.js",
    "start:ssr": "cd dist/mc-auth-app/server && node server.mjs",
    "build-and-start": "npm run build && npm run start:ssr"
  }
}

Here's a brief overview of each script:

build: Executes the Angular build process (ng build) and then runs the postbuild.js script to apply necessary modifications to the generated server.mjs file.

start:ssr: Changes the working directory to dist/mc-auth-app/server and starts the server using node server.mjs.

build-and-start: Combines the build and serve steps by running the build script followed by the start:ssr script.

Implementing these scripts has streamlined my development workflow and resolved the issues encountered with the generated server.mjs file.

Please note that this is a temporary workaround for a known issue in the implementation. A permanent fix should be applied once the underlying bug is resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants