英文:
How to import objects from custom library with deep export
问题
我已更新我的库结构(添加了一些目录),现在无法从中导入类型。
这是库的结构:
|-dist(已构建的TypeScript)
|-src
|--business
|---entities
|----user.ts
|----site.ts
|---usecases
|----user.usecase.ts
|----site.usecase.ts
|---entities.ts
|---usecases.ts
|--infrastructure
|---...
|--index.ts
|-package.json
user.ts
和 site.ts
包含类和接口(usecases
部分也一样)。
entities.ts
导出了这些类型(usecases
部分也一样):
export { Site, SiteInterface } from "src/business/entities/site.js";
export { User, UserInterface } from "src/business/entities/user.js";
index.ts
从子目录导出所有内容:
export * from "src/business/entities.js";
export * from "src/business/usecases.js";
在 user.ts
中,我可以这样导入 Site
:import { Site } from "src/index.js";
但是当我在另一个应用程序中安装我的库时,我会得到上面的错误消息:“模块 '
我是这样导入的:import { Site, User } from '@xxx/<my-lib>';
以下是库的配置文件:
package.json
{
"name": "@xxx/<my-lib>",
"version": "0.0.0-alpha.1",
"type": "module",
"main": "src/index.js",
"scripts": {
"clean": "rm -rf dist",
"build": "npm run clean && tsc --build",
"test": "jest",
"circular-deps": "madge --ts-config ./tsconfig.json --extensions ts --circular ./src"
},
"publishConfig": {
"registry": "https://gitlab.com/api/v4/projects/<id>/packages/npm/"
},
"devDependencies": {
"@babel/cli": "^7.22.6",
"@babel/core": "^7.22.8",
"@babel/preset-env": "^7.22.7",
"@babel/preset-typescript": "^7.22.5",
"@types/node": "^20.2.5",
"babel-jest": "^29.6.1",
"babel-plugin-module-resolver": "^5.0.0",
"jest": "^29.6.1",
"ts-jest": "^29.1.1",
"ts-jest-resolver": "^2.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
tsconfig.json
{
"compilerOptions": {
"composite": true,
"target": "esnext",
"module": "esnext",
"moduleResolution": "nodenext",
"baseUrl": ".",
"paths": {
"@xxx/<my-lib>": ["dist/src/"],
"@xxx/<my-lib>/*": ["dist/src/*"]
},
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["src/**/*", "package.json"],
"exclude": ["node_modules/**/*"]
}
有人能帮助我理解我做错了什么吗?
英文:
I have update my library structure (adding some directories), and now I can't import types from it.
Here is the structure of the library :
|-dist (builded typescript)
|-src
|--business
|---entities
|----user.ts
|----site.ts
|---usecases
|----user.usecase.ts
|----site.usecase.ts
|---entities.ts
|---usecases.ts
|--infrastructure
|---...
|--index.ts
|-package.json
user.ts
and site.ts
contains class and interface (same for usecases
part).
entities.ts
exports these types (same for usecases
part) :
export { Site, SiteInterface } from "src/business/entities/site.js";
export { User, UserInterface } from "src/business/entities/user.js";
index.ts
exports all from subdirectories :
export * from "src/business/entities.js";
export * from "src/business/usecases.js";
In user.ts
, I can import Site
like this : import { Site } from "src/index.js";
But when I install my library in another application, I get the above error Module '"<my-lib>"' has no exported member '<my-type>'
.
I import it like this : import { Site, User } from '@xxx/<my-lib>';
Here are library's config files :
package.json
{
"name": "@xxx/<my-lib>",
"version": "0.0.0-alpha.1",
"type": "module",
"main": "src/index.js",
"scripts": {
"clean": "rm -rf dist",
"build": "npm run clean && tsc --build",
"test": "jest",
"circular-deps": "madge --ts-config ./tsconfig.json --extensions ts --circular ./src"
},
"publishConfig": {
"registry": "https://gitlab.com/api/v4/projects/<id>/packages/npm/"
},
"devDependencies": {
"@babel/cli": "^7.22.6",
"@babel/core": "^7.22.8",
"@babel/preset-env": "^7.22.7",
"@babel/preset-typescript": "^7.22.5",
"@types/node": "^20.2.5",
"babel-jest": "^29.6.1",
"babel-plugin-module-resolver": "^5.0.0",
"jest": "^29.6.1",
"ts-jest": "^29.1.1",
"ts-jest-resolver": "^2.0.1",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
tsconfig.json
{
"compilerOptions": {
"composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
"target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"module": "esnext", /* Specify what module code is generated. */
"moduleResolution": "nodenext", /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": ".", /* Specify the base directory to resolve non-relative module names. */
"paths": { /* Specify a set of entries that re-map imports to additional lookup locations. */
"@xxx/<my-lib>": ["dist/src/"],
"@xxx/<my-lib>/*": ["dist/src/*"],
},
"resolveJsonModule": true, /* Enable importing .json files. */
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
"declarationMap": true, /* Create sourcemaps for d.ts files. */
"outDir": "dist", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
},
"include": ["src/**/*", "package.json"],
"exclude": ["node_modules/**/*"]
}
Does someone can help me to understand what I do wrong please ?
答案1
得分: 0
Gitlab不在各个阶段之间共享工作空间,因此它发布了空包。我已经纠正了这个问题,现在它可以正常工作。
我也根据我的问题中的评论更新了我的包。
感谢您的帮助,@rveerd 和 @aluan-haddad
英文:
Ok I have found where was the problem :
Gitlab does not share workspace between stages, so it publish empty package. I have corrected this, and now it works.
I update my package following comments on my question too.
Thank you for your help @rveerd and @aluan-haddad
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论