【问题标题】:Module not found while importing nested module导入嵌套模块时找不到模块
【发布时间】:2019-09-25 19:14:18
【问题描述】:

假设我们有package1package2。如果package2 尝试导入package1 模块,我们会收到一条错误消息module not found。导入看起来像import { ... } from 'package1/styles'。请记住,像 import { ... } from 'package1' 这样的导入是有效的。在我看来,这些导入问题是由于 lerna 错误地链接文件夹引起的。我希望package2node_modules 应该只包含package1/dist 而不是package1。这些导入问题应该如何解决?

相关package1文件如下。

package.json:

...
"main": "./dist/index.js",
"files": ["dist"]
...
tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "noImplicitAny": false,
    "declaration": true,
    "jsx": "react",
    "allowJs": false,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}


在我们运行lerna run build 之后,我们得到以下目录结构:

packages/package2/node_modules/package1/
├── dist
│  ├── index.d.ts
│  ├── index.js
│  └── styles
│     ├── index.d.ts
│     └── index.js
├── node_modules
├── package-lock.json
├── package.json
├── src
│  ├── index.ts
│  └── styles
│     └── index.ts
├── tsconfig.json
└── webpack.config.js

【问题讨论】:

    标签: node.js typescript lerna


    【解决方案1】:

    tsconfig.json 应该具有解析模块依赖关系的路径,例如:

    ...
        "baseUrl": "./packages",
        "paths": {
          "package1": ["./package1/src"],
          "package1/*": ["./package1/src/*"]
        }
    ...
    
    

    【讨论】:

      猜你喜欢
      • 2012-03-07
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2016-09-06
      • 1970-01-01
      相关资源
      最近更新 更多