Just dipping my toes into typescript and grappling with module resolution. The problem seems straightforward (or so I think), but there's something off about my tsconfig.json
.
If my folder structure looks like this:
+ release
+ definitions
+ js
> a.js
> main.js
+ src
> main.ts
> a.ts
Using gulp for incremental compilation, following the example in gulp documentation.
Here's how my tsconfig.json
is set up:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"src/*": [
"./src/*"
]
}
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"**/*.spec.ts"
]
}
I'm trying to create aliases for the src directory as src/module_name
, so that import resolutions work smoothly, like in import foo from 'src/la/ja'
. The aim is to avoid convoluted paths like
import bar from '../../../utils/bar'
by simply using from 'src/utils/bar'
.
In my main.ts
, I have:
import { a } from 'src/a'
I expect the compiler to pick up on the correct relative path. However, despite the seemingly correct definitions and absence of linting errors with my current tsconfig.json
, the compiled output ends up as:
const a_1 = require("src/a");
Instead of:
const a_1 = require("./a");
This leads to runtime errors because src/
cannot be found in release/js/
. Is there a way to compel typescript to compile to relative paths?