node.js - 保留导入包的目录结构

标签 node.js typescript import

我正在尝试创建一个目录结构如下的包:

src/
    folder1/
        index.ts
        ...
    folder2/
        index.ts
        ...
    folder3/
        index.ts
        ...
.
.
.

我想将其导入为 import { Class1 } from '@package/folder1'import { Class2 } from '@package/folder2' 等。

此时,我可以导入我的类,例如 import { Class1 } from '@package/lib/folder1'。是否有可能实现我的预期行为?

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./lib",
    "rootDir": "./src",
    "rootDirs": ["./src/folder1"],
    "lib": [
      "es2017",
      "esnext.asynciterable"
    ],
    "typeRoots": [
      "./node_modules/@types"
    ],
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "pretty": true,
    "noEmit": false,
    "esModuleInterop": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "lib"]
}

最佳答案

在我使用的 Typescript 版本(3.7.4)中,这个功能似乎并不是开箱即用的。包 module-alias 可能是一种解决方法,但看起来 Typescript 3.8 将允许向导出添加别名。

关于node.js - 保留导入包的目录结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60154110/

相关文章:

javascript - 在 nodejs SQLite3 .each() 方法中,如何添加多个参数以处理多个 "WHERE ... = ?"查询?

Python "from [dot]package import ..."语法

node.js - 如果另一个文件已需要该文件,我是否需要 'require'

javascript - Number.isNaN ('asd' ) === 在 Node.js 中为 false!是什么赋予了?

performance - Redis,边读边写,用NodeJS

angular2 使用嵌套导入模块的组件

typescript - 火力地堡功能 : Unhandled error RangeError: Maximum call stack size exceeded

angular - 错误 TS2339 : Property 'scan' does not exist on type 'Observable<Message[]>'

java - 尝试在Java中导入vk.core.api

maven-2 - poms 中的 Maven 重用