I have the problem where my TypeScript aliased imports work in some files, but not in others even in the same directory.
In Entity.ts
and Fusion.ts
, I want to import from ../enums/EntityType
. Relative imports like these can get messy so I am making use of aliasing.
So, instead, I want to alias like import { EntityType } from '@enums/EntityType'
.
This works fine in Entity.ts
. However, it doesn’t work in Fusion.ts
. I get the error:
throw err;
^
Error: Cannot find module '@enums/EntityType'
Require stack:
- C:codestore-order-managerdistmodelsFusion.js
- C:codestore-order-managerdistserviceslocation-service.js
- C:codestore-order-managerdistindex.js
In my tsconfig.json
file:
"compilerOptions": {
/* Language and Environment */
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
/* Modules */
"module": "commonjs" /* Specify what module code is generated. */,
"rootDir": "./src" /* Specify the root folder within your source files. */,
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": ".", /* Specify the base directory to resolve non-relative module names. */
"paths": {,
"@enums/*": ["./src/enums/*"]
} /* Specify a set of entries that re-map imports to additional lookup locations. */,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
},
"include": ["src/**/*"]
}
Why do I get errors with some files, but not with other files in the same directory? I run my project with:
rimraf ./dist
tsc
node dist/index.js
From the error logs, I can see it is erroring in the .js
files, leading me to believe that perhaps the transpilation over to JavaScript is the issue.
Here is the relevant transpiled JavaScript in dist/models/Entity.js
(Entity.ts
works fine with the aliased import):
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
And here is the relevant transpiled JavaScript in dist/models/Fusion.js
(Fusion.ts
doesn’t work with the aliased import):
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const EntityType_1 = require("@enums/EntityType");