我如何创建一个项目(依赖),我可以在其他项目之间共享?



我有一个项目a

// projectA/tsconfig.json
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"outDir": "./build",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
}
}
// projectA/package.json
{
"name": "project_a",
"version": "1.0.0",
"main": "index.js",
"devDependencies": {
"typescript": "^4.7.4"
}
}
// projectA/src/main.ts
export function projectAHello() {
console.log("Hello from project A!")
}

我正在尝试在项目B中导入它;

// projectB/tsconfig.json
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"outDir": "./build",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"moduleResolution": "node",
}
}
// projectB/package.json
{
"name": "project_b",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"project_a": "../projectA"
},
"devDependencies": {
"typescript": "^4.7.4"
}
}
// projectB/src/main.ts
import projectAHello from "project_a/main"
projectAHello()

但是当我在projectB中运行编译器时,它给了我这个:

❯ ./node_modules/.bin/tsc
src/main.ts:1:27 - error TS2307: Cannot find module 'project_a/main' or its corresponding type declarations.

那么我如何正确配置项目A,以便项目B可以使用它的代码?

您可以使用npm link本地链接包。你可以在这里查看文档。

相关内容