ts-node' module_not_found'在打字稿中使用绝对导入时
我偶然发现了一个绝对导入的问题。存储库可在此处公开可用: https://github.com/repetitioestmatioestmaterestmaterestmatestmaterestmaterestmatudiorum/ts-boiler
我正在尝试导入具有绝对路径的文件(相对于项目目录),然后执行npm Run Dev
或NPM运行TS-Node ts-node src/index.ts
i我会收到以下错误:(
Error: Cannot find module '/src/constants'
Require stack:
- /Users/<my-username>/<some-path>/ts-boiler/src/index.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/<my-username>/<some-path>/ts-boiler/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/<my-username>/<some-path>/ts-boiler/src/index.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module.m._compile (/Users/<my-username>/<some-path>/ts-boiler/node_modules/ts-node/src/index.ts:1597:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Object.require.extensions.<computed> [as .ts] (/Users/<my-username>/<some-path>/ts-boiler/node_modules/ts-node/src/index.ts:1600:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/<my-username>/<some-path>/ts-boiler/src/index.ts' ]
}
出于隐私原因,我的用户名和文件夹结构被混淆)
相对导入,例如从'./constants'
内部的import {c} import
。 TS 工作正常。将其更改为从'/src/startants'
或import> import> import {c}从'src/constants'
带有相应的tsconfig.json < /代码>设置,我会收到错误。 到导入时,也会发生同样的错误)
(当我附加.js
或.ts
:
"baseUrl": ".",
"paths": {
/* Support absolute imports with a leading '/' */
"/*": ["*"]
},
我通常与Meteorjs合作,最近遵循教程进行混音(要了解框架)。这两个框架都鼓励绝对导入,我将它们的两个tsconfig.json
文件复制到我的项目中(在混音的设置中添加〜
)来尝试查看他们的配置对我有用 - 无成功!
我还考虑了如何启用绝对导入的内容: https://javascript.plainenglish.io/why-and-how-to-so-so-sosolute-imports-imports-in-react-d5b52f24d53c 导致相同的错误。
对我来说更令人困惑的是,配置了ESLINT的VSCODE并不抱怨tsconfig.json
文件中具有正确设置的绝对导入。
奇怪的是,有一个导入使用一个绝对路径,可以在项目内部使用相同的设置在项目内部运行良好:从'/types/t.constants'
中导入类型{常数{常数}。它还可以使用“类型”,例如从'/types/t.constants'
导入{常数}。可能是因为导入的文件不在“ src/”中,而是在“ type/”中?
也许有人解决了一次类似的问题?
I stumbled upon a problem with absolute imports. The repository is publicly available here: https://github.com/repetitioestmaterstudiorum/ts-boiler
When I'm trying to import a file with absolute path (relative to the project directory) and then execute npm run dev
or npm run ts-node src/index.ts
I am getting the following error:
Error: Cannot find module '/src/constants'
Require stack:
- /Users/<my-username>/<some-path>/ts-boiler/src/index.ts
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._resolveFilename.sharedData.moduleResolveFilenameHook.installedValue [as _resolveFilename] (/Users/<my-username>/<some-path>/ts-boiler/node_modules/@cspotcode/source-map-support/source-map-support.js:811:30)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:102:18)
at Object.<anonymous> (/Users/<my-username>/<some-path>/ts-boiler/src/index.ts:1:1)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Module.m._compile (/Users/<my-username>/<some-path>/ts-boiler/node_modules/ts-node/src/index.ts:1597:23)
at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Object.require.extensions.<computed> [as .ts] (/Users/<my-username>/<some-path>/ts-boiler/node_modules/ts-node/src/index.ts:1600:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/<my-username>/<some-path>/ts-boiler/src/index.ts' ]
}
(my username and folder structure is obfuscated for privacy reasons)
Relative imports, such as import { C } from './constants'
inside the file src/index.ts
work fine. When changing that to import { C } from '/src/constants'
or import { C } from 'src/constants'
with the corresponding tsconfig.json
settings, I'm getting the error.
(the same error also occurs when I append .js
or .ts
to the import)
tsconfig.json
settings for absolute imports with leading dash enabled:
"baseUrl": ".",
"paths": {
/* Support absolute imports with a leading '/' */
"/*": ["*"]
},
I usually work with MeteorJS and recently followed the tutorial for Remix (to get to know the framework). Both of these frameworks encourage absolute imports and I copied both of their tsconfig.json
files into my project (adding ~
in the case of Remix's settings) to try and see if their configuration would work for me - without success!
I also looked at this on how to enable absolute imports: https://javascript.plainenglish.io/why-and-how-to-use-absolute-imports-in-react-d5b52f24d53c which resulted in the same error.
What's more confusing for me is that VSCode with ESLint configured does not complain about the absolute imports with the right settings in the tsconfig.json
file.
And weirdly, there is one import that uses an absolute path that works well inside the project with the same settings: import type { Constants } from '/types/t.constants'
. It also works without "type", e.g. import { Constants } from '/types/t.constants'
. It could be because the imported file is not in "src/" but in "types/"?
Maybe someone solved a similar problem once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更多的研究,我在这里找到了答案: https://medium.com/@fmoessle/typescript-paths-with-with-node-node-node-ts-node-node-dev-and-jest-671deacf6428
我案中需要的是
npm i -d tsconfig-paths
,然后将以下内容添加到我的tsconfig.json:有关此的更多信息: https://github.com/typestrong/ts-node#paths-and-baseurl
从
src/code>目录工作中,将绝对导入到jest tests中的目录好吧,我需要将以下内容添加到
jest.config.ts
:After more research I found my answer here: https://medium.com/@fmoessle/typescript-paths-with-ts-node-ts-node-dev-and-jest-671deacf6428
What was required in my case was
npm i -D tsconfig-paths
and then adding the following to my tsconfig.json:More about this: https://github.com/TypeStrong/ts-node#paths-and-baseurl
To make absolute imports from the
src/
directory work in JEST tests as well, I needed to add the following tojest.config.ts
: