gpt4 book ai didi

node.js - 带有 Node.js 的 Typescript baseUrl

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:38 24 4
gpt4 key购买 nike

为了避免 import 中的长路径,我在我的 tsconfig.json 中使用了 Typescript baseUrl 选项

这是我的 tsconfig.json:

{
"compilerOptions": {
"target": "ES6",
"module": "none",
"removeComments": true,
"rootDir": "./",
"outDir": "Build",
"moduleResolution": "node",
"noImplicitAny": true,
"pretty": true,
"baseUrl": "./"
},
"exclude": [
"node_modules",
"Build"
]
}

所以不要这样做

import foo from "../../../../hello/foo"

我这样做

import foo from "hello/foo"

它在 Typescript 编译器中运行良好,但是当我使用 node.js 运行我的应用程序时,出现此错误:

module.js:474
throw err;
^

Error: Cannot find module 'hello/foo'

P.s:我不想像我在互联网上看到的那样替换 require() 函数

那么我怎样才能使 node.js 与 baseUrl 一起工作或使 typescript 将 "hello/foo" 之类的路径替换为 "../../../../hello/foo" ?

Typescript 编译器版本:

Version 2.3.0-dev.20170303

最佳答案

在使用 node.js 运行应用程序时传递 NODE_PATH env 参数

例子:

set NODE_PATH=./src
node server.js

关于node.js - 带有 Node.js 的 Typescript baseUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42582807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com