How to import the module relative to the root of the project in typescript?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
15 views
/app |--/src | |--currentfile.ts | |--anotherfile.ts | |--/env |--environment.ts


currentfile.ts
import { another } from './anotherfile'; import { environment } from 'env/environment';


How to configure tsconfig.js so, to import such modules work? Import the current problems, i.e. anotherfile imported successfully. But when trying to import from the root of the project, namely the file env/environment, webpack complains that the module could not be found in /app/src
baseUrl: '.' installed

UPD.
Noticed that the problem occurs when you import an Injectable class. Don't understand how this affects the import relative to the root of the project.
by | 15 views

1 Answer

0 like 0 dislike
The problem was solved in the following way.
Perhaps inattentively read the documentation.
Briefly:
In tsconfig.ts prescribe:
{ "compilerOptions": { "baseUrl": ".", // the root of your project, in this case the directory where config "paths": [ "env/*": ["env/*"] ] } }

Read more:
Imports relative to the root of work subject to the availability of in tsconfig.json properties baseUrl and paths.
Namely, if you specify the import
import { environment } from 'env/environment';
no properties listed above in the config, the plugin env/environment webpak is relative to the file where the import was made.
To import relative to the project root in the config file you need to register
{ "compilerOptions": { "baseUrl": ".", // the root of your project, in this case the directory where config "paths": [ "env/*": ["env/*"] ] } }

Thus, meeting the import, starting with env/, webpack will look for the module relative to the baseUrl
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
110,608 questions
257,187 answers
0 comments
40,796 users