简体   繁体   English

如何在 Lerna monorepo 中从 package 加载 package?

[英]How do I load a package from a package in a Lerna monorepo?

I have:我有:

packages
 -models
   -package.json
   -....
 -server
   -src
     -index.ts
   -package.json

In my packages/server/package.json , I have:在我的packages/server/package.json中,我有:

  "scripts": {
    "dev": "ts-node src/index.ts"
  },
  "dependencies": {
    "@myapp/models": "../models",

In my packages/server/src/index.ts , I have:在我的packages/server/src/index.ts中,我有:

import { sequelize } from '@myapp/models'

In my packages/models/src/index.ts , I have:在我的packages/models/src/index.ts中,我有:

export type UserAttributes = userAttr


export { sequelize } from './sequelize';

but it gives me an error:但它给了我一个错误:

  Try `npm install @types/myapp__models` if it exists or add a new declaration (.d.ts) file containing `declare module '@myapp/models';`

 import { sequelize } from '@myapp/models'

How do I get this to work properly?我怎样才能让它正常工作?

Lerna will take care of the dependencies between your local packages, you just need to make sure you set them up correctly. Lerna 将处理本地包之间的依赖关系,您只需要确保正确设置它们即可。 The first thing I would suggest is to go to @myapp/models and make sure that your package.json contains the fields you will need: main and more importantly types (or typings if you prefer):我建议的第一件事是typings@myapp/models并确保您的package.json包含您需要的字段(或者更重要的是,键入: main和更重要的types

// packages/models/package.json

{
  // ...
  "main": "dist/index.js",
  "types": "dist/index.d.ts",
  // ...
}

As you can see I made both of them point to some dist folder, which takes me to my second point - you will need to build every package as if it was a separate NPM module outside of the monorepo.正如你所看到的,我让它们都指向某个dist文件夹,这将我带到我的第二点 - 你需要构建每个 package,就好像它是 monorepo 之外的一个单独的 NPM 模块一样。 I am not saying you need the dist folder, where you build it is up to you, you just need to make sure that from the outside your @myapp/models exposes main and types and that these are valid and existing .js and .d.ts files.我并不是说你需要dist文件夹,你在哪里构建它取决于你,你只需要确保你的@myapp/models从外部公开maintypes并且这些是有效的和现有的.js.d.ts文件。

Now for the last piece of the puzzle - you need to declare your @myapp/models dependency as if it was a "real" package - you need to specify its version rather than point to a folder:现在对于拼图的最后一块 - 你需要声明你的@myapp/models依赖,就好像它是一个“真实的” package - 你需要指定它的版本而不是指向一个文件夹:

// packages/server/package.json

{
  "dependencies": {
    // ...
    "@myapp/models": "0.0.1" // Put the actual version from packages/models/package.json here
    // ...
  }
}

Lerna will notice that this is a local package and will install & link it for you. Lerna 会注意到这是一个本地 package 并将为您安装和链接它。

I don't know Lerna, but a good tool to deal with monorepos is npm link .我不知道 Lerna,但处理 monorepos 的一个好工具是npm link

  1. cd packages/models cd 包/模型
  2. npm link npm链接
  3. cd packages/server cd 包/服务器
  4. restore the version in dependencies "@myapp/models": "xyz",恢复依赖项中的版本"@myapp/models": "xyz",
  5. npm link @myapp/models npm 链接@myapp/models

It should be enough.应该够了。

Hope this helps.希望这可以帮助。

lerna bootstrap first then yarn add <package_name> works Or lerna bootstrap first, then add the package and the target version then run yarn. lerna bootstrap first then yarn add <package_name> works 或者 lerna bootstrap first, 然后添加 package 和目标版本,然后运行 yarn。

After you put in the dependencies in your server's package.json, you just need to run将依赖项放入服务器的 package.json 中后,只需运行

lerna run --stream build

And your local should be able to access to it.您的本地人应该能够访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM