I've added a path alias to my TypeScript project to make the importing a bit more manageable, however, I get an error when I run the compiled JavaScript.
When I run: node ./bin/index.js
The response is:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '@castorApp/CastorApp'
This way I import is:
import { CastorApp } from "@castorApp/CastorApp";
my folder structure is:
CastorJS/
├─ node_modules/
├─ bin/
├─ src/
│ ├─ castorApp/
│ │ ├─ CastorApp.ts
│ │ ├─ CommandService.ts
│ ├─ types/
│ ├─ index.ts
├─ .gitignore
├─ package-lock.json
├─ package.json
├─ tsconfig.json
my tsconfig.json looks like this:
{
"compilerOptions": {
"module": "commonjs",
"target": "es2017",
"lib": [
"es2015"
],
"moduleResolution": "node",
"sourceMap": true,
"outDir": "bin",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
],
"@castorApp*": [
"src/castorApp",
"src/castorApp*"
]
},
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": [
"src/**/*"
]
}
My package.json looks like this:
{
"name": "castor",
"version": "1.0.0",
"description": "TypeScript version of Castor",
"main": "bin/index.js",
"scripts": {
"build": "tsc",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "ZtModArchive",
"license": "MIT",
"dependencies": {
"@types/node": "^18.7.18"
},
"devDependencies": {
"typescript": "^4.8.3"
}
}
I've seen someone on a blog post recommend the module-alias package, however the package has the following warning on its readme:
WARNING: This module should not be used in other npm modules since it modifies the default require behavior! It is designed to be used for development of final projects i.e. web-sites, applications etc.
And so, since I do wish to publish my code as a package, this is unfortunately not an option.