[英]Typescript: Resolve Package from different location than node_modules
I am learning typescript and I would love to begin experimenting packages creation. 我正在学习打字稿,很想开始尝试创建软件包。
Here is my folder structure for the moment: 这是我目前的文件夹结构:
myProject/
├── node_modules/
├── src/
│ ├── app
│ ├── index.ts
│ ├── packages
│ ├── database
│ ├── index.ts
├── package.json
├── tsconfig.json
As you can see, my src
folder is divided into app
that will contain my application implementation and a package
folder that is supposed to be more "abstract", in order to become one day a package eventually. 正如你所看到的,我的
src
文件夹分为app
将包含我的应用程序实现和一个package
被认为是更“抽象”的文件夹,才能成为一天一个包最终。
What I would like, would be to access to my packages modules writing the following in my app folder: 我想要的是访问我的包模块,将以下内容写入我的app文件夹:
import Database from 'packages/database';
instead of 代替
import Database from '../packages/database/index';
I looked around the paths
configuration into the tsconfig.json
file but I could not get it to work: 我在
tsconfig.json
文件中查看了paths
配置,但无法正常工作:
{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"packages": ["src/packages"]
}
}
}
I would also like to keep access to the node_modules
folder of course... 我当然也想继续访问
node_modules
文件夹...
How could I achieve that? 我该如何实现?
Thank your for your replies 多谢您的回覆
I think that the best solution for you would be to divide your work into several packages: 我认为,最适合您的解决方案是将您的工作分成几个包:
For example, you would have a package-database package that would make all the database related work for you. 例如,您将拥有一个软件包数据库软件包,它将为您完成所有与数据库相关的工作。
In order to do that, you need to modify your structure a little bit: 为此,您需要稍微修改一下结构:
myProject/
├── node_modules/
├── src/
│ ├── app
│ ├── index.ts
│ ├── packages
│ ├── database
│ ├── SomeClass.ts
│ ├── index.ts
├── package.json
├── tsconfig.json
Then make all the exports in your database/index.ts
file: 然后将所有导出文件添加到
database/index.ts
文件中:
import SomeClass from './SomeClass'
import SomeOtherClass from './SomeOtherClass'
export {SomeClass, SomeOtherClass}
You would the be able to access it from your app folder typing: 您将能够从您的应用文件夹中输入以下内容来访问它:
import {SomeClass} from 'package-database';
Of course, you also need to modify your tsconfig.json
, you were close to it: 当然,您还需要修改
tsconfig.json
,您已经很接近它了:
{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"package-database": ["src/packages/database"]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.