【问题标题】:Export enum from user defined typescript path result in Module not found从用户定义的打字稿路径导出枚举导致找不到模块
【发布时间】:2020-09-17 16:40:21
【问题描述】:

我已经定义了this thread 中回答的自定义路径。

"baseUrl": ".",
"paths": {
  "@library/*": [
    "./src/myFolder/*"
  ],
}

从这个模块中,我导出了一个枚举。

export enum ENUM {
   ONE = "one"
}

当尝试将它与自定义路径一起使用时,我得到了

找不到模块:无法解析 /path/to/src 中的 @path/I/imported/from

直接用'../relative/path'导入没问题

这里是a link to minimal project 显示问题。 (编辑:我改代码停止使用create-react-app,访问localhost:3400时出错)

我第一次在express的一个节点项目中遇到了这个问题。

我猜这个问题来自于 typescript 类型不包含在编译的 js 中,因此枚举中存在的值不存在。 没有警告说这个“一”值可能不包括在内,这似乎仍然很奇怪。

我的问题是使用自定义路径和导出枚举的最佳方法是什么?

【问题讨论】:

    标签: javascript typescript enums


    【解决方案1】:

    哦,我阅读了编辑后的问题并检查了您的项目。以下是我所做的更改:

    我使用了tsconfig-paths 模块:这将允许使用根导入别名正确构建应用程序并映射到文件系统中的物理路径。

    更新的 tsconfig.json

    {
      "compilerOptions": {
        "target": "es6",
        "preserveConstEnums": true,  //<----change
        "module": "commonjs",
        "outDir": "dist",
        "sourceMap": true,
        "baseUrl": "./src", //<----change
        "esModuleInterop": true,
        "paths": {
          "@library/*": [
            "myFolder/*"  //<----change
          ],
        }
      },
      "include": [
        "src/**/*"
      ],
      "exclude": [
        "node_modules",
        ".vscode"
      ]
    }
    
    

    更新的 package.json

    {
      "name": "test2",
      "version": "1.0.0",
      "description": "",
      "main": "src/index.ts",
      "scripts": {
        "start:dev": "nodemon",
        "start": "node --inspect=5858 -r tsconfig-paths/register -r ts-node/register ./src/server.ts",  //<----change
        "build": "tsc"
      },
      "author": "",
      "license": "ISC",
      "dependencies": {
        "express": "^4.17.1",
        "tsconfig-paths": "^3.9.0" //<----change
      },
      "devDependencies": {
        "@types/express": "^4.17.8",
        "@types/node": "^14.11.1",
        "concurrently": "^5.3.0",
        "nodemon": "^2.0.4",
        "ts-node": "^9.0.0",
        "typescript": "^4.0.2"
      },
      "nodemonConfig": {
        "ignore": [
          "**/*.test.ts",
          "**/*.spec.ts",
          ".git",
          "node_modules"
        ],
        "watch": [
          "src"
        ],
        "exec": "npm start",
        "ext": "ts"
      }
    }
    

    还有一个名为 tsconfig-paths-bootstrap.js 的附加文件(与 tsconfig.json 相邻),内容如下:

    const tsConfig = require('./tsconfig.json');
    const tsConfigPaths = require('tsconfig-paths');
    const baseUrl = './build';
    tsConfigPaths.register({
        baseUrl,
        paths: tsConfig.compilerOptions.paths,
    });
    
    

    一切顺利! 希望对您有所帮助!

    【讨论】:

    • 感谢您的回复,但问题不是来自 cra,我使用 cra 快速创建了一个项目,但是在使用 firebase 函数时出现了问题。我编辑了 github 项目以使用 express 显示问题。
    • 哦,知道了,我根据编辑的问题更改答案。希望有帮助!!!
    • 谢谢,似乎不需要 preserveConstEnums 选项来使其工作。
    【解决方案2】:

    这是因为 create-react-app 不支持路径别名,并且在不久的将来可能不会。

    请查看commentthread

    【讨论】:

    • 感谢您的回复,但问题不是来自 cra,我使用 cra 快速创建了一个项目,但是在使用 firebase 函数时出现了问题。我编辑了 github 项目以使用 express 显示问题。
    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 2017-11-11
    • 2022-01-19
    • 2021-05-03
    • 2019-12-06
    • 2019-02-12
    相关资源
    最近更新 更多