When using npm workspaces, how to force a package to be installed in the relative node_modules?
Asked Answered
O

1

7

Here are some related questions:

When using yarn workspaces, how to force a package to be installed in the relative node_modules?

NPM 7 Workspaces - Multiple node_modules?

Should I have to use no-hoist for all packages in a monorepo with react-native-web?

I'm using npm workspaces to organise multiple packages. The issue is that my main package don't have its dependency's(which is also one of the workspaces) source code in local node_modules. I know the dependency is installed in root node_modules, the thing is that I need to visit it by relative path from the main package.

Here is the project structure after running npm install in root dir:

root
├── package.json ->  { "workspaces": ["packages/*"] }
├── node_modules
│   ├── dependency-A
│   ├── dependency-B
└── packages
    ├── main-package
    ├── dependency-A
    └── dependency-B

package.json in root dir:

{
  "workspaces":  [
      "packages/main-package",
      "packages/dependency-A",
      "packages/dependency-B"
    ]
}

package.json in "packages/main-package":

{
  "dependencies": {
    "dependency-A": "0.1.0",
    "dependency-B": "0.1.0"
  }
}

webpack.config.js in "packages/main-package":

{
  plugins: [
      new CopyWebpackPlugin({
        patterns: [
          {
            from: "node_modules/dependency-A/media",
            to: "static/dependency-A-media",
          },
        ],
      }),
      new CopyWebpackPlugin({
        patterns: [
           {
             context: "node_modules/dependency-B/dist",
             from: "research-data.json",
           },
         ],
      }),
  ]
}

When I run webapck in main-package, the error message is:

ERROR in unable to locate '/Users/trumangao/myApp/packages/main-package/node_modules/dependency-A/media' glob
ERROR in unable to locate '/Users/trumangao/myApp/packages/main-package/node_modules/dependency-B/dist/research-data.json' glob

I'm wondering what is the best practice to resolve such question? The option "nohoist" of Yarn inspired me but I can't find it in npm. I also tried to run install in package dir but it will break their symlink. How could I install dependencies of each package in their local node_modules while maintain their links like lerna@4?

Tried versions: [email protected] [email protected] & [email protected] [email protected]

Hope I've made myself plain with my poor English LoL, thanks a lot.

Oubre answered 14/2, 2023 at 10:13 Comment(0)
U
0

I was able to solve my similar issue by doing a replace all of "node_modules/foopkg with "newpath/node_modules/foopkg, then running npm i, and repeating until errors are resolved. but this is probably not an appropriate solution. Maybe this repo can help? https://github.com/zackerydev/noist

Unamerican answered 22/7 at 17:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.