Cra*_*shi 5 node.js typescript visual-studio-code tsconfig-paths
我正在运行 VS Code,目前正在尝试在我的打字稿项目上设置一些别名。
我的开发设置依赖于 nodemon 和 ts-node,代码被编译到一个 dist 文件夹。
到目前为止,我成功地让 Typescript Hero 使用别名来管理导入:
到目前为止,我的文件夹结构是:
.
??? src
???modules
?????Category
?????Ressource
???shared
?????debug
Run Code Online (Sandbox Code Playgroud)
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"pretty": true,
"sourceMap": true,
"target": "es6",
"outDir": "./dist",
"baseUrl": "./src",
"paths": {
"@shared/*": [
"shared/*"
],
"@modules/*": [
"modules/*"
]
},
"resolveJsonModule": true,
"esModuleInterop": true
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"**/*.spec.ts",
"**/*.test.ts",
]
}
Run Code Online (Sandbox Code Playgroud)
这是第一个失败的别名导入。
//Server.ts file
import Print from '@shared/debug/Print.class';
import App from './App';
const MyApp: App = new App();
MyApp.ExpressApp.listen(MyApp.Config.ExpressPort, () => {
Print.Log('Express server listening on port ' + MyApp.Config.ExpressPort);
});
Run Code Online (Sandbox Code Playgroud)
但是,我在“cross-env NODE_ENV=development nodemon ts-node ./src/server.ts”上收到错误消息: “找不到模块‘@shared/debug/Print.class’ ”。

这就是我的立场。
现在,我已经阅读了一些关于 SO 的问答,似乎即使我在开发中设法使别名正常工作,它也会在生产中失败,因为我从 Typescript src 文件夹运行,而我的可交付成果是在 dist 中构建的? 如果是这样,有什么办法可以补救吗?非常感谢
1N5*_*4N3 27
我遇到了同样的问题,CrazyYoshi 解决方案在我的情况下无法正常工作。
我通过将此代码放入来修复它tsconfig.json:
{
"ts-node": {
// Do not forget to `npm i -D tsconfig-paths`
"require": ["tsconfig-paths/register"]
}
}
Run Code Online (Sandbox Code Playgroud)
之后不要忘记安装tsconfig-paths/register模块:
npm i -D tsconfig-paths
Run Code Online (Sandbox Code Playgroud)
文档: https: //typestrong.org/ts-node/docs/paths/
Sha*_*ahi 15
您正在使用ts-node并nodemon运行您的代码,因此您应该将其添加到您的tsconfig.json文件中:
{
"ts-node": {
"require": ["tsconfig-paths/register"]
}
}
Run Code Online (Sandbox Code Playgroud)
然后tsconfig-paths/register作为开发依赖项安装。
npm install --save-dev tsconfig-paths
Run Code Online (Sandbox Code Playgroud)
Cra*_*shi 14
问题出在运行时的节点路径别名解析上。即使 ts-node 在运行时执行打字稿,节点也无法按原样解析别名。(我认为)
但这只是冰山一角。稍后我会在我的笑话设置和 JS 运行时遇到它。
对于我拥有的每个运行时,我必须找到一种方法来解释我的别名。有几个 npm 包,但很多都需要更多的声明。
而且我不想在我拥有的每个配置文件中声明我的别名,并且只依赖于我的 tsconfig 文件。
经过大量测试,只有两个节点模块可以安装tsconfig-paths以在 ts-node 上执行typescript运行时。和@ef-carbon/tspm将我的别名转换为构建目标。
npm i -D tsconfig-paths @ef-carbon/tspm
Run Code Online (Sandbox Code Playgroud)
对于ts-node,脚本修改为:
ts-node -r tsconfig-paths/register ./src/server.ts
Run Code Online (Sandbox Code Playgroud)
对于你编译的 js,你只需要运行:
ef-tspm
Run Code Online (Sandbox Code Playgroud)
开玩笑,需要ts-jest,我已经有了它,但没有正确配置。我使用内置的助手来设置我的路径。我的笑话配置文件现在看起来像这样:
//jest.config.js
const { pathsToModuleNameMapper } = require('ts-jest/utils');
const { compilerOptions } = require('./tsconfig');
module.exports = {
roots: ['<rootDir>/src'],
globals: {
'ts-jest': {
tsConfig: 'tsconfig.json',
diagnostics: {
warnOnly: true,
},
},
},
clearMocks: true,
coverageDirectory: 'coverage',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
moduleFileExtensions: ['js', 'json', 'jsx', 'node', 'ts', 'tsx'],
testEnvironment: 'node',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/src/' }),
pathToJest: 'npm test',
preset: 'ts-jest',
testMatch: null,
};
Run Code Online (Sandbox Code Playgroud)
这是我的脚本在 package.json 中的样子
"scripts": {
"dev:ts": "cross-env NODE_ENV=development nodemon",
"dev:js": "cross-env NODE_ENV=development npm run start:js",
"staging": "cross-env NODE_ENV=staging npm run start:js",
"production": "cross-env NODE_ENV=production npm run start:js",
"test": "cross-env NODE_ENV=testing jest --runInBand",
"test:debug": "npm run test --detectOpenHandles",
"start:js": "npm run build && nodemon --config nodemon-js.json",
"build": "npm run compile && npm run post:compile && npm run copyAssets",
"compile": "tsc",
"post:compile": "ef-tspm",
"copyAssets": "copyfiles -e ./src/**/*.ts -e ./src/**/*sample* -e ./src/**/*.json -u 1 ./src/**/* ./dist/"
},
Run Code Online (Sandbox Code Playgroud)
看看它是怎么回事,我可能会在之后添加一个 grunt/gulp 解决方案。但就目前而言,这已经足够了。
2023 年 3 月
只是花了几个小时试图让它发挥作用。最后,我完全放弃了 ts-node 并安装了开箱即用的tsx 。
显然,这最终会出现在 ts-node 上,尽管 PR 看起来已经死了,所以谁知道呢:
https://github.com/TypeStrong/ts-node/pull/1585
同时,如果您确实想坚持使用 ts-node,您可能会发现以下有用:
https://www.npmjs.com/package/@bleed- believer/path-alias
https://github.com/TypeStrong/ts-node/issues/1007
https://github.com/TypeStrong/ts-node/discussions/1450#discussioncomment-1806115
我认为你可以使用名为的包module-alias来解决这个问题。
首先,使用yarn、npm或pnpm安装它,选择你使用的一个。
然后,将相同的别名添加到您的中package.json,如下所示:
{
"_moduleAliases": {
"@shared": "./src/shared",
"@modules": "./src/modules"
}
}
Run Code Online (Sandbox Code Playgroud)
最后,导入module-alias/register你的第一行(这很重要)!
import 'module-alias/register'
// ... your other code
Run Code Online (Sandbox Code Playgroud)
这是地址,module-alias你可以参考一下。
希望我的回答能够帮助您解决问题。
| 归档时间: |
|
| 查看次数: |
8401 次 |
| 最近记录: |