打字稿:如何为node.js解析绝对模块路径? [英] Typescript: How to resolve absolute modules paths for node.js?

查看:30
本文介绍了打字稿:如何为node.js解析绝对模块路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于 baseUrl 解析模块,以便输出代码可用于node.js

I need modules to be resolved basing on baseUrl so output code is usable for node.js

这是我的 src/server/index.ts

import express = require('express');
import {port, databaseUri} from 'server/config';

...

这是我的 src/server/config/index.ts

export const databaseUri: string = process.env.DATABASE_URI || process.env.MONGODB_URI;
export const port: number = process.env.PORT || 1337;

运行 tsc 我能够编译所有文件而没有错误,但是输出: dist/server/index.js

Running tsc I'm able to compile all files without erros, but output: dist/server/index.js is

"use strict";
var express = require("express");
var config_1 = require("server/config");

...

生成的结果如果我尝试将其与 node dist/sever/index.js 一起使用,则找不到模块"server/config" .

Resulting with Cannot find module 'server/config' if I'm trying to use it with node dist/sever/index.js.

为什么无法以任何方式解析 server/config 路径,因此可以使用已编译的代码或如何使其得以解析.还是我在做什么或在想错误的方式?

Why server/config path is not resolved in any way so it would be possible to use compiled code or how to make it resolve it. Or what am I doing or thinking wrong way?

我的 tsc --version 2.1.4

这是我的 tsconfig.json :

{
  "compileOnSave": true,
  "compilerOptions": {
      "baseUrl": "./src",
      "rootDir": "./src",
      "module": "commonjs",
      "target": "es5",
      "typeRoots": ["./src/types", ".node_modules/@types"],
      "outDir": "./dist"
  },
  "include": [
      "src/**/*"
  ],
  "exclude": [
      "node_modules",
      "**/*.spec.ts"
  ]
}

请注意,我 不要 要使用 ../../../../relative 路径.

推荐答案

这篇文章微软打字稿上的github解释了他们的模块解析过程.他们在评论中解释说,您尝试做的事情无法完成.

This post on Microsoft's typescript github explains their module resolution process. In the comments they explain that what you're trying to do can't be done.

此功能以及模块其余部分的分辨率功能,仅是为了帮助编译器找到模块源给定模块名称.无需更改输出js代码.如果您需要"folder2/file1"将始终以这种方式发出.你可能会得到如果编译器找不到folder2/file1.ts,但没有则报错更改为输出. https://github.com/Microsoft/TypeScript/issues/5039#issuecomment-206451221

编译器不会重写模块名称.模块名称是考虑的资源标识符,并在它们映射到输出时出现在来源中 https://github.com/Microsoft/TypeScript/issues/5039#issuecomment-232470330

因此,从打字稿中发出的JS不会为您提供给 require 的发现模块重写模块路径.如果您在编译后在 node 中运行您的应用程序(看起来与express一样),则它将使用

So, the emitted JS from typescript does NOT rewrite the module path for the discovered modules you give to require. If you run your app in node after compilation (which it looks like you are with express), then it will use the node module system to resolve the module references after typescript compilation. This means that it will only respect relative paths in your module and then it will fall back to node_modules to find dependencies.

这就是它的工作方式.编译器需要找到的路径模块的声明.模块名称是资源标识符并且应按原样发出,且不得更改. https://github.com/Microsoft/TypeScript/issues/5039#issuecomment-255870508

您基本上已经在问题的输出中为自己确认了这一点.

You have basically confirmed this for yourself in the emitted output in your question.

这篇关于打字稿:如何为node.js解析绝对模块路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆