I am trying to use custom paths to simplify the import of monly used modules, and I set this config:
// tsconfig.json
{
"pilerOptions": {
"module": "monjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "build",
"allowJs": true,
"target": "es5",
"sourceMap": true,
"baseUrl": ".",
"paths": {
"config": ["app/config"]
}
},
"exclude": [
"node_modules”,
"build"
]
}
I tried to import the config module using "config", but the app failed in requiring the config the file. The require path inside the piled file is still "config".
// result:
var config = require("config");
// what is should be:
var config = require("../../config");
Even thought the module resolution log show that it has been resolved.
======== Resolving module 'config' from '/abs/path/routes/internal/signin/index.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
'baseUrl' option is set to '/abs/path', using this value to resolve non-relative module name 'config'
'paths' option is specified, looking for a pattern to match module name 'config'.
Module name 'config', matched pattern 'config'.
Trying substitution 'config', candidate module location: 'config'.
Loading module as file / folder, candidate module location '/abs/path/config', target file type 'TypeScript'.
File '/abs/path/config.ts' does not exist.
File '/abs/path/config.tsx' does not exist.
File '/abs/path/config.d.ts' does not exist.
File '/abs/path/config/package.json' does not exist.
File '/abs/path/config/index.ts' exist - use it as a name resolution result.
======== Module name 'config' was successfully resolved to '/abs/path/config/index.ts'. ========
What am I missing to make the path changes after pilation to point to the right module?
I am trying to use custom paths to simplify the import of monly used modules, and I set this config:
// tsconfig.json
{
"pilerOptions": {
"module": "monjs",
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"outDir": "build",
"allowJs": true,
"target": "es5",
"sourceMap": true,
"baseUrl": ".",
"paths": {
"config": ["app/config"]
}
},
"exclude": [
"node_modules”,
"build"
]
}
I tried to import the config module using "config", but the app failed in requiring the config the file. The require path inside the piled file is still "config".
// result:
var config = require("config");
// what is should be:
var config = require("../../config");
Even thought the module resolution log show that it has been resolved.
======== Resolving module 'config' from '/abs/path/routes/internal/signin/index.ts'. ========
Module resolution kind is not specified, using 'NodeJs'.
'baseUrl' option is set to '/abs/path', using this value to resolve non-relative module name 'config'
'paths' option is specified, looking for a pattern to match module name 'config'.
Module name 'config', matched pattern 'config'.
Trying substitution 'config', candidate module location: 'config'.
Loading module as file / folder, candidate module location '/abs/path/config', target file type 'TypeScript'.
File '/abs/path/config.ts' does not exist.
File '/abs/path/config.tsx' does not exist.
File '/abs/path/config.d.ts' does not exist.
File '/abs/path/config/package.json' does not exist.
File '/abs/path/config/index.ts' exist - use it as a name resolution result.
======== Module name 'config' was successfully resolved to '/abs/path/config/index.ts'. ========
What am I missing to make the path changes after pilation to point to the right module?
Share Improve this question asked Apr 10, 2017 at 13:15 Garsallah mohamedGarsallah mohamed 1561 gold badge1 silver badge10 bronze badges 1- 1 Did you find a solution? – Pablo Commented Sep 27, 2017 at 17:57
3 Answers
Reset to default 3Apparently paths
was never intended to actually resolve the url to its relative version. You are supposed to do this using a post-processor of some sort. I am using this Babel plugin.
You can use this code to fix non-relative imports: obs: install tsconfig-paths and ts-node
"scripts": {
"start": "node -r tsconfig-paths/register -r ts-node/register ./dist/app.js",
}
It's up to the bundling to perform this action, see also tsconfig-replace-paths. It does replace the references.
E.g.
"scripts": {
"build": "tsc --project tsconfig.json
&& tsconfig-replace-paths --project tsconfig.json",
}
However, you will still be left with the bundling problem. See, https://github./microsoft/TypeScript/issues/26565