Skip to content

Undetermined shared module version when importing a file from the module directly #13457

Closed

Description

@Hornwitser

Bug report

What is the current behavior?

When importing a file directly from a module (i.e. import "react-dom/test-utils") when it's configured to be shared via ModuleFedrationPlugin a warning with a nonsensical instruction may be emitted when running a build:

WARNING in shared module react-dom
No required version specified and unable to automatically determine one. Unable to find required version for "react-dom" in description file (/home/user/test/node_modules/react-dom/package.json). It need to be in dependencies, devDependencies or peerDependencies.

If the current behavior is a bug, please provide the steps to reproduce.

Using the following webpack config

"use strict";
const path = require("path");
const webpack = require("webpack");


module.exports = (env = {}) => ({
    entry: "./index.js",
    output: {
        publicPath: "auto",
        filename: "bundle.js",
        path: path.resolve(__dirname, "dist"),
    },
    mode: "production",
    plugins: [
        new webpack.container.ModuleFederationPlugin({
            name: "test",
            shared: {
                "react-dom": { singleton: true },
            },
        }),
    ],
});

And an index.js file with the content

import "react-dom/test-utils"

Along with a package.json file with the dependencies described in reproduces the behaviour. See the complete example in this repository.

What is the expected behavior?

I expect either the version of react-dom to be correctly deduced from package.json or a sensible warning/error message.

Other relevant information:
webpack version: 5.37.1
Node.js version: 16.0.0
Operating System: Arch Linux
Additional tools:

Activity

  1. alexander-akait commented on May 27, 2021

    @alexander-akait
    Member

    Expected, you want to share react-dom, but request react-dom/test-utils, change on "react-dom/test-utils": { singleton: true },, but I think we need improve error message here

  2. Hornwitser commented on May 27, 2021

    @Hornwitser
    ContributorAuthor

    Maybe, but in my case it was a dependency of a dependency that imported react-dom/test-utils and I can't deduce that from the error message. And I want react-dom, not react-dom/test-utils, as the former is what I care about and what is imported everywhere.

    I just tried adding both react-dom and react-dom/test-utils to the shared config and the error is still shown. Using only react-dom/test-utils is not an option as I need react-dom to be shared.

  3. arrgson commented on Jun 22, 2021

    @arrgson

    I have the same issue happening with @angular/common .

    shared module @angular/common - Warning: No required version specified and unable to automatically determine one. Unable to find required version for "@angular/common" in description file (.../node_modules/@angular/common/package.json). It need to be in dependencies, devDependencies or peerDependencies.

    In the package.json file there is no entry for @angular/common under those properties.

  4. brendonco commented on Jul 1, 2021

    @brendonco

    @sokra does webpack version 5.41.1 fix this issue?

  5. alexander-akait commented on Jul 1, 2021

    @alexander-akait
    Member

    No, you can send a fix

  6. flyyuan commented on Jul 28, 2021

    @flyyuan

    same problem.

  7. webpack-bot commented on Oct 27, 2021

    @webpack-bot
    Contributor

    This issue had no activity for at least three months.

    It's subject to automatic issue closing if there is no activity in the next 15 days.

  8. added
    status: staleNo author response after a needs: request; close candidate
    on Oct 27, 2021
  9. deepaknegi10 commented on Nov 8, 2021

    @deepaknegi10

    Getting similar issue, any fixes?

    WARNING in shared module react
    No required version specified and unable to automatically determine one. Unable to find required version for "react" in description file (/Users/deepaknegi/Desktop/projects/email-template-ui/node_modules/@emotion/react/jsx-runtime/package.json). It need to be in dependencies, devDependencies or peerDependencies.

  10. removed
    status: staleNo author response after a needs: request; close candidate
    on Nov 8, 2021
  11. 30 remaining items

  12. Bairon29 commented on Oct 16, 2024

    @Bairon29

    I had the same issue with “deep-object-diff” and solved it with this. Notice the required version and version.
    new ModuleFederationPlugin({ …, shared: { …packageJSONDeps, “deep-object-diff”: { singleton: true, version: packageJSONDeps[“deep-object-diff”], requiredVersion: packageJSONDeps[“deep-object-diff”] }}}

  13. krutoo commented on Oct 18, 2024

    @krutoo
  14. alexander-akait commented on Oct 21, 2024

    @alexander-akait
    Member

    @krutoo Yeah, because there is a bug in @sentry/browser, look at node_modules/@sentry/browser/build/npm/esm/package.json:

    {"type":"module","sideEffects":false}

    No name and version.

    From docs:

    If you plan to publish your package, the most important things in your package.json are the name and version fields as they will be required.

    If you plan to publish your package, the most important things in your package.json are the name and version fields as they will be required.

    https://docs.npmjs.com/cli/v10/configuring-npm/package-json

    The package is published, and it is necessary to specify these fields, even if it is used for switching the type.

    They already have:

    "exports": {
       "./package.json": "./package.json",
       ".": {
         "import": {
           "types": "./build/npm/types/index.d.ts",
           "default": "./build/npm/esm/index.js"
         },
         "require": {
           "types": "./build/npm/types/index.d.ts",
           "default": "./build/npm/cjs/index.js"
         }
       }
     },
    

    Therefore, the presence of an additional files is not even required, but can only break the logic of determining versions and names.

    I don't think we are making a mistake here, rather the opposite, we are allowing this problem to be caught. The only solution is to specify the version manually, you can read the package.json of @sentry/browser so you don't have to write it manually.

    I would advise you to open an issue in this repository and refer to this issue, such problems will most likely appear in any bundlers (which support module federation), and even most likely in other tools.

  15. krutoo commented on Oct 22, 2024

    @krutoo

    @alexander-akait but name and version is defined in root package.json

    according to this logic, all information from the root package.json should be duplicated

    I did this in my packages and specified the version, but then module federation complained about the lack of dependency versions

  16. alexander-akait commented on Oct 22, 2024

    @alexander-akait
    Member

    @krutoo

    according to this logic, all information from the root package.json should be duplicated

    No, package.json do not inherit information from the previous one

    Any package.json should have name and version if it is published, even for nested, this is the rule

  17. krutoo commented on Oct 22, 2024

    @krutoo

    @alexander-akait @sentry/node has exactly same structure and same nested package.json, when you import it in Node.js .mjs file it works fine

    I think the rule you are talking about applies only to the main package.json

  18. alexander-akait commented on Oct 22, 2024

    @alexander-akait
    Member

    From npm docs - any published package.json, it doesn't matter where placed should have this information. Moreover, you can publish a whole mono repo and have multiple packages in a package. I can change this logic here but it will most likely break other examples and will be a breaking change

  19. eemeli commented on Oct 22, 2024

    @eemeli
    Contributor

    The npm docs on this are misleading, and unfortunately my attempt to clarify them was recently closed by @wraithgar: npm/documentation#134.

  20. alexander-akait commented on Oct 23, 2024

    @alexander-akait
    Member

    @eemeli - Yeah I see, because changing the resolver logic can be dangerous without spec/docs and because developers close this issue I would interpret this as mandatory fields always for any published projects

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: module-federationModule Federation (lib/container, lib/sharing)status: staleNo author response after a needs: request; close candidatewebpack-5Concerns the webpack 5 line

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions