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

Module Not Found when trying to share a service . #11

Open
piyushdanej opened this issue Apr 3, 2021 · 0 comments
Open

Module Not Found when trying to share a service . #11

piyushdanej opened this issue Apr 3, 2021 · 0 comments

Comments

@piyushdanej
Copy link

piyushdanej commented Apr 3, 2021

Issue Report .

I am trying to share a service's singleton instance across projects but I am getting Module not found Error .
I followed step by step tutorial .

tsconfig.json app level

        {
          "compileOnSave": false,
          "compilerOptions": {
            "paths": {
              "astrapay-library": [
                "dist/astrapay-library/astrapay-library",
                "dist/astrapay-library"
                // "projects/astrapay-library/src/project.ts"
              ]
            },
            "baseUrl": "./",
            "outDir": "./dist/out-tsc",
            "sourceMap": true,
            "declaration": false,
            "downlevelIteration": true,
            "experimentalDecorators": true,
            "moduleResolution": "node",
            "importHelpers": true,
            "target": "es2015",
            "module": "es2020",
            "lib": [
              "es2018",
              "dom"
            ]
          },
          "angularCompilerOptions": {
            "enableI18nLegacyMessageIdFormat": false,
            // "strictTemplates" : true
          }
        }

Shell app's webpack.config.json

            [...]
            const sharedMappings = new mf.SharedMappings();
            sharedMappings.register(
              path.join(__dirname, '../../tsconfig.json'),
              [
                /* mapped paths to share */
                "astrapay-library"   // <-- This is my shared library's name
              ]);
        
        
        module.exports = {
          output: {
            uniqueName: "shell"
          },
          optimization: {
            // Only needed to bypass a temporary bug
            runtimeChunk: false
          },
          plugins: [
            new ModuleFederationPlugin({
              
                // For remotes (please adjust)
                // name: "shell",
                // filename: "remoteEntry.js",
                // exposes: {
                //     './Component': './projects/shell/src/app/app.component.ts',
                // },        
                
                // For hosts (please adjust)
                remotes: {
                    "payment": "payment@http://localhost:3000/remoteEntry.js",
                },
        
                shared: {
                  "@angular/core": { singleton: true, strictVersion: true }, 
                  "@angular/common": { singleton: true, strictVersion: true }, 
                  "@angular/router": { singleton: true, strictVersion: true },
                  "ngx-toastr" : {singleton: true, strictVersion: true},
                  ...sharedMappings.getDescriptors()
                }
                
            }),
            sharedMappings.getPlugin(),
          ],
        };

payment app's webpack.config.json

        [...] 
        const sharedMappings = new mf.SharedMappings();
        sharedMappings.register(
          path.join(__dirname, '../../tsconfig.json'),
          [
            /* mapped paths to share */
            "astrapay-library"
          ]);
        
        module.exports = {
          output: {
            uniqueName: "payment"
          },
          optimization: {
            // Only needed to bypass a temporary bug
            runtimeChunk: false
          },
          plugins: [
            new ModuleFederationPlugin({
              
                // For remotes (please adjust)
                name: "payment",
                filename: "remoteEntry.js",
                exposes: {
                    './BankManagememt': './projects/payment/src/modules/bank-management/bank-management.module.ts',
                },        
                
                // For hosts (please adjust)
                // remotes: {
                //     "shell": "shell@http://localhost:5000/remoteEntry.js",
        
                // },
        
                shared: {
                  "@angular/core": { singleton: true, strictVersion: true }, 
                  "@angular/common": { singleton: true, strictVersion: true }, 
                  "@angular/router": { singleton: true, strictVersion: true },
          
                  "ngx-toastr" : {singleton: true, strictVersion: true},
                  ...sharedMappings.getDescriptors()
                }
                
            }),
            sharedMappings.getPlugin(),
          ],
        };

When running ng serve payment and ng serve shell I get this error :
image

Meanwhile if I dont specify the library in sharedMappings.register( "dirpath", --here--) and specify the library in shared object like this :

        shared: {
          "@angular/core": { singleton: true, strictVersion: true }, 
          "@angular/common": { singleton: true, strictVersion: true }, 
          "@angular/router": { singleton: true, strictVersion: true },
          "astrapay-library" : {
            singleton: true,
            import : "projects/astrapay-library/src/project"
          },
          "ngx-toastr" : {singleton: true, strictVersion: true},
          ...sharedMappings.getDescriptors()
        }

It works but the service is not singleton .
Why am I getting this issue ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant