javascript - typescript 路径映射未在 Node 应用程序中翻译

标签 javascript node.js typescript ts-node

目前在 node.js 应用程序中使用 typescript 。我已经编译了我的应用程序,并且正在使用 typescript's path mapping feature .

'api/*' 应该在根文件夹中查找。在开发工作中使用以下内容:

nodemon --watch src -e ts,tsx --exec ts-node -r tsconfig-paths/register --disableWarnings ./src/index.ts

tsconfig-paths/register 允许正确翻译 require 并且 ts-node 将正确执行/运行应用程序。现在,当我转向生产时,问题就来了。过去在生产中,我只是在文件夹上运行 tsc,然后将 outDir (dist/) 的内容移动到我的 docker 镜像中的/app,然后运行 ​​node/app/index .js。在我开始使用 typescript 的路径映射功能之前,这一直有效。现在我只是得到错误:

Error: Cannot find module 'api/module/path/here'

来自 this github comment模块名称显然没有映射到输出编译的 javascript。

我的 tsconfig.json 文件:

{
    "compilerOptions": {
      "target": "es2015",
      "module": "commonjs",
      "moduleResolution": "node",
      "allowSyntheticDefaultImports": true,
      "jsx": "react",
      "allowJs": true,
      "alwaysStrict": true,
      "sourceMap": true,
      "forceConsistentCasingInFileNames": true,
      "noFallthroughCasesInSwitch": true,
      "noImplicitReturns": true,
      "noUnusedLocals": true,
      "noUnusedParameters": true,
      "noImplicitAny": false,
      "noImplicitThis": false,
      "strictNullChecks": false,
      "experimentalDecorators": true,
      "emitDecoratorMetadata": true,
      "lib": ["es2017", "dom"],
      "baseUrl": "src",
      "outDir": "dist",
      "types": [
        "node"
      ],
      "paths": {
        "universal/*": ["../../universal/*"],
        "api/*": ["*"],
        "*": ["node_modules/*"]
      },
      "typeRoots": [
        "./node_modules/@types",
        "./src/types"
      ]
    },
    "include": [
      "src/**/*"
    ]
}

在 node.js 环境中使用相对路径映射的推荐方法是什么?如果 typescript 是解决问题的方法,为什么不重写 require 语句呢?仅仅为了添加 typescript 提供的模块解析功能而涉及另一个步骤(如 babel 或 webpack)感觉很愚蠢。

编辑:在进一步挖掘之后,我发现我可以在我的 Node 环境中使用 -r tsconfig-paths/register(我只需要复制到我的 tsconfig.json 文件中)。我可以将 docker 中的入口点切换为:

ENTRYPOINT [ "node", "-r", "tsconfig-paths/register", "index.js" ]

问题是,我现在需要修改我的 tsconfig.json baseUrl,因为目录 src/ 不存在。此外,我注意到该解决方案不适用于模块“util”(它显然在我的 node_modules 文件夹中使用 util 而不是 node.js util 库),这导致我的应用程序崩溃。

最佳答案

根据您提供的来源,您可以使用(例如,将最后一个路径解析为第一个路径):

"rootDirs": [
      "src/api/module/path/here",
      "api/module/path/here"
    ]

The problem is, I now need to modify my tsconfig.json baseUrl, as the directory src/ doesn't exist.

typescript's path mapping feature状态:

The flexibility of rootDirs is not limited to specifying a list of physical source directories that are logically merged. The supplied array may include any number of ad hoc, arbitrary directory names, regardless of whether they exist or not. This allows the compiler to capture sophisticated bundling and runtime features such as conditional inclusion and project specific loader plugins in a type safe way.

此外,您是否尝试过跟踪模块解析:tsc --traceResolution

关于javascript - typescript 路径映射未在 Node 应用程序中翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47913699/

相关文章:

Node.JS 加载一个 html 页面,填写表单,然后按下提交按钮

javascript - 如何使用 Angular Material 在具有可扩展行的表中创建嵌套垫表

javascript - 将 PWA 添加到 Angular 8 - 模块构建失败

javascript - 在 qooxdoo 中为图像添加 CSS 类

javascript - 如何在不写入数据的情况下返回 Vue 资源?

javascript - 变量不影响 setInterval 循环的速度

javascript - 如何使用http-server创建NodeJS服务器?

javascript - 有没有更快的方法来设置下拉列表中的选定值?

javascript - 预过滤条件 - deep-diff Node.js

node.js - TypeORM 无法将 cli 与配置一起使用