【问题标题】:LernaJs tsc Cannot find moduleLernaJs tsc 找不到模块
【发布时间】:2020-08-11 21:20:24
【问题描述】:

我正在使用 lerna 创建一个全栈 monorepo 项目。

在其中一个子项目中我得到了Cannot find module '@gepick/database/connect'

我的 lerna.json

{
  "packages": ["packages/*", "projects/**"],
  "version": "0.0.0",
  "useWorkspaces": true,
  "npmClient": "yarn"
}

我的gepick/database/package.json:

{
  "name": "@gepick/database",
  "version": "1.0.0",
  "devDependencies": {
    "@types/lodash": "^4.14.149",
    "@types/mongoose": "^5.7.11",
    "source-map-loader": "^0.2.4"
  },
  "dependencies": {
    "lodash": "^4.17.15",
    "mongodb-client-encryption": "^1.0.1",
    "mongoose": "^5.9.9"
  }
}

基于节点的子项目 package.json

{
  "name": "scripts-api-football",
  "version": "1.0.0",
  "scripts": {
    "start:collectDayMatches:dev": "tsc-watch --onSuccess \"node -r source-map-support/register build/collectDayMatches.js\"",
    "build": "tsc"
  },
  "devDependencies": {
    "@types/lodash": "^4.14.149",
    "source-map-loader": "^0.2.4",
    "source-map-support": "^0.5.19",
    "tsc-watch": "^4.2.3"
  },
  "dependencies": {
    "@gepick/database": "^1.0.0",
    "axios": "^0.19.2",
    "commander": "^5.0.0",
    "lodash": "^4.17.15",
    "nodemon": "^2.0.3"
  }
}

基于节点的子项目 tsconfig.json

{
  "compilerOptions": {
    "target": "es2019",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "dist",
    "strict": true,
    "inlineSourceMap": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "strictNullChecks": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "allowJs": false,
    "lib": ["es2016", "esnext.asynciterable", "webworker"],
    "types": ["node"],
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strictPropertyInitialization": false,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": "./src",
    "paths": {
      "utils/*": ["./utils/*"]
    }
  },
  "exclude": ["tests", "tasks"]
}

主包.json:

{
  "name": "gepick-lerna",
  "version": "1.0.0",
  "private": true,
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start:front-web": "lerna run start --scope front-web --stream",
    "bootstrap": "lerna bootstrap",
    "build:scripts:api-football": "lerna run build --scope scripts-api-football --stream",
    "start:scripts:api-football:start:collectDayMatches:dev": "lerna run start:collectDayMatches:dev --stream"
  },
  "dependencies": {
    "lerna": "^3.20.2",
    "typescript": "^3.8.3",
    "webpack": "^4.42.1"
  },
  "workspaces": [
    "projects/**",
    "packages/*"
  ],
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^2.27.0",
    "@typescript-eslint/parser": "^2.27.0",
    "eslint": "^6.8.0",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-jsx-a11y": "^6.2.3",
    "eslint-plugin-react": "^7.19.0",
    "eslint-plugin-react-hooks": "^2.5.1"
  }
}

另外,我尝试将main: "connect.js" 添加到@gepick/datebase package.json 文件中。但看起来 @gepick/datebase 包没有编译 js。也许是问题(tsc 不编译库文件)?

【问题讨论】:

  • 这能回答你的问题吗? LernaJS Typescript cannot find module
  • 看看lerna bootstrap是否解决了你的问题
  • 我尝试将 main 选项添加到 package.json。另外,我尝试从根文件夹运行 lerna bootsrap 。但是应用程序仍然找不到模型。但问题可能是 tsc 根本没有编译 lerna 包,这就是找不到它的原因。

标签: node.js typescript lerna


【解决方案1】:

问题是包没有编译成js。我将 node 更改为 ts-node 用于运行脚本。现在可以了。

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 2022-10-14
    • 2018-03-06
    • 2016-06-14
    • 2020-09-15
    • 2016-09-29
    • 1970-01-01
    • 2018-11-08
    • 2021-03-20
    相关资源
    最近更新 更多