admin管理员组文章数量:1317906
I am using typescript v3.6.4
with the following tsconfig.json
snippet:
"pilerOptions": {
"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
"@config/*": ["config/*"],
"@config": ["config"],
}
}
and module alias in package.json:
"_moduleAliases": {
"@config": "dist/config"
}
I have the following folder structure:
src
|-config
|-index.ts
|-app
|index.ts
|logic.ts
|-dist
Now in app/index.ts, if I do:
import 'module-alias/register';
import config from '@config';
and my npm start
mands is:
"start": "node -r ts-node/register ./src/app/index.ts",
tsc
will pile successfully but npm start
will give error:
Error: Cannot find module '@config' in src/app/logic.ts
And the only way to fix this is to also add
import 'module-alias/register';
in src/app/logic.ts
Seems I have to add the import 'module-alias/register'
in every file that I do alias? Is it a way to configure this?
I am using typescript v3.6.4
with the following tsconfig.json
snippet:
"pilerOptions": {
"moduleResolution": "node",
"baseUrl": "./src",
"paths": {
"@config/*": ["config/*"],
"@config": ["config"],
}
}
and module alias in package.json:
"_moduleAliases": {
"@config": "dist/config"
}
I have the following folder structure:
src
|-config
|-index.ts
|-app
|index.ts
|logic.ts
|-dist
Now in app/index.ts, if I do:
import 'module-alias/register';
import config from '@config';
and my npm start
mands is:
"start": "node -r ts-node/register ./src/app/index.ts",
tsc
will pile successfully but npm start
will give error:
Error: Cannot find module '@config' in src/app/logic.ts
And the only way to fix this is to also add
import 'module-alias/register';
in src/app/logic.ts
Seems I have to add the import 'module-alias/register'
in every file that I do alias? Is it a way to configure this?
- 1 Does the error only occur if you build and run the app? – Daniel Habenicht Commented Oct 30, 2019 at 21:57
- @DanielHabenicht the build works fine, it only happens on run time. – jamesdeath123 Commented Oct 30, 2019 at 22:48
-
1
That's what I meant. You have to import the
module-alias/register
only once in youapp/index.ts
it should load all other imports in e.g. inapp/logic.ts
without the need to import it there again. That's also what their docs say. – Daniel Habenicht Commented Oct 30, 2019 at 23:24 -
That's needed because the typescript piler does not update the paths in the generated files (with what you specified in the
tsconfig.ts
). That's why you need to loadmodule-alias/register
and of course in order to make it work you have to at least import in once, otherwise the packages code would not be included. – Daniel Habenicht Commented Oct 30, 2019 at 23:30 - If that solved it for you I will right an answer, so please let me know. – Daniel Habenicht Commented Oct 30, 2019 at 23:58
2 Answers
Reset to default 3@2021
For me I've got exact the same issue: typescript allowed me only use the module aliasing when the every module has import 'module-alias/register';
at its top line when it has such module alias.
After spent nearly whole day, I got the point:
tsc
won't help to transpiling these decorators. so you have to make sure every consumer of the js piled code have acknoledged and know how to deal with these module imports with alias.
i.e.:
node index.js
=>
node -r module-alias/register index.js
///
mocha .
=>
mocha -r module-alias/register .
The solution to this is quite simple:
in the root file, like app/index.ts, put the import 'module-alias/register';
to the end of all the other imports and you won't need to put it in every file. It didn't work for me because I put that import before all other imports, not after.
For example:
import express from 'express';
import corsFilter from 'cors';
...
import 'module-alias/register'; // This has to be after all other imports
本文标签: javascripttypescript path alias needs modulealiasregister on every fileStack Overflow
版权声明:本文标题:javascript - typescript path alias needs module-aliasregister on every file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742030450a2416345.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论