[英]How do I import an individual module in a monorepo project in JavaScript/NPM
I have a monorepo with the following file structure:我有一个具有以下文件结构的 monorepo:
➜ yw git:(master) tree
.
├── package.json
├── packages
│ ├── common
│ │ ├── package.json
│ │ ├── src
│ │ │ ├── add.ts
│ │ │ ├── index.ts
│ │ │ └── multiply.ts
│ │ └── tsconfig.json
│ └── server
│ ├── package.json
│ └── src
│ └── index.ts
└── tsconfig.json
Inside packages/common/src/add.ts
I have:在
packages/common/src/add.ts
我有:
export const add = (a: number, b:number) => {
return a + b
}
I have the multiply version also defined in packages/common/src/multiply.ts
.我还在
packages/common/src/multiply.ts
中定义了乘法版本。
In packages/common/src/index.ts
I imported and re-exported the modules在
packages/common/src/index.ts
我导入并重新导出了模块
export * from './add'
export * from './multiply'
The content of packages/common/package.json
is: packages/common/package.json
的内容为:
{
"name": "common",
"version": "1.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"type": "commonjs",
"license": "MIT",
"devDependencies": {
"typescript": "^4.5.2"
},
"scripts": {
"build": "tsc"
}
}
In common
project I run yarn
and then yarn build
, which generated packages/common/dist
with content:在
common
项目中,我运行yarn
然后yarn build
,它生成了带有内容的packages/common/dist
:
➜ yw git:(master) ✗ tree packages/common/dist
packages/common/dist
├── add.d.ts
├── add.js
├── add.js.map
├── index.d.ts
├── index.js
├── index.js.map
├── multiply.d.ts
├── multiply.js
└── multiply.js.map
now in packages/server/src/index.ts
I have:现在在
packages/server/src/index.ts
我有:
import { add } from 'common';
console.log(add(1,2))
And in packages/server/package.json
, I have:在
packages/server/package.json
中,我有:
{
"name": "server",
"version": "1.0.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"license": "MIT",
"dependencies": {
"common": "1.0.0"
},
"devDependencies": {
"typescript": "^4.5.2"
},
"scripts": {
"build": "tsc"
}
}
So in server
project, I am depending on common
- as can be seen in the dependency.所以在
server
项目中,我依赖于common
——从依赖项中可以看出。 This works, because when I run npx ts-node packages/server/src/index.ts
it consoles log 3.这行得通,因为当我运行
npx ts-node packages/server/src/index.ts
它控制台日志 3。
The question now is, how can I change the dependency definition in packages/server/src/index.ts
from:现在的问题是,如何更改
packages/server/src/index.ts
中的依赖定义:
import { add } from 'common';
console.log(add(1,2))
to至
import { add } from 'common/add';
console.log(add(1,2))
That is, import from the add
module directly: import { add } from 'common/add';
即直接从
add
模块导入: import { add } from 'common/add';
. .
Any ideas on how to achieve this?关于如何实现这一目标的任何想法?
As @morganney said, you need to manually map individual modules to their equivalent bundle through exports
in common/package.json
.正如@morganney 所说,您需要通过
common/package.json
中的exports
手动将 map 单个模块添加到其等效捆绑包中。 I'm answering based on a related dev.to article .我根据相关的dev.to 文章回答。
(I don't think there's globbing pattern support.) (我认为没有通配模式支持。)
{
"exports": {
"./add": "dist/add.js",
"./multiply": "dist/multiply.js"
}
}
You also have to manually specify each module's typing:您还必须手动指定每个模块的类型:
{
"typesVersions": {
"*": {
"./add": ["dist/add.d.ts"],
"./multiply": ["dist/multiply.d.ts"]
}
}
}
I've not played with this, but the article above on dev.to also puts a package.json
path in "exports":我没有玩过这个,但上面 dev.to 上的文章还在“出口”中放置了
package.json
路径:
{
"exports": {
"./add": "dist/add.js",
"./multiply": "dist/multiply.js",
"./package.json": "./package.json"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.