For a couple of days I have been trying to carry out a project where its main structure is:
tsconfig output folder:
./build
/prisma
/master
functions.js
types.js
/src
/Controllers
/Master
UsersControllers.js
/Utils
/prisma
index.js
/Routes
/Protected
/MasterRouter
/users
index.js
index.js
index.js
/Server
index.js
index.js
project root:
./Build
./prisma
/master
functions.ts
types.ts
./src
/Controllers
/Master
UsersControllers.ts
/Utils
/prisma
index.ts
/Routes
/Protected
/MasterRouter
/users
index.ts
index.ts
index.ts
/Server
index.ts
index.ts
where the tsconfig part is placed as follows
"rootDir": "./"
"moduleResolution": "Node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"baseUrl": "./"
"paths": {
"@controllers/*": ["src/Controllers/*"],
"@dataDefault/*": ["src/Data/*"],
"@middlewares/*": ["src/Middlewares/*"],
"@routes/*": ["src/Routes/*"],
"@proyectTypes/*": ["src/ProyectTypes/*"],
"@utils/*": ["src/Utils/*"],
"@server/*": ["src/Server/*"],
"@prismaUtils/*": ["prisma/*"]
}
"outDir": "./build"
We have the script in the package.json as follows with the aliases of the modules:
"scripts": {
"build": "tsc",
"start-app": "cross-env NODE_ENV=production node -r module-alias/register build/src/index.js",
"start": "npm run build && npm run start-app",
"dev": "NODE_ENV=development nodemon"
}
"module-alias": {
"@controllers": "src/Controllers",
"@dataDefault": "src/Data",
"@middlewares": "src/Middlewares",
"@routes": "src/Routes",
"@proyectTypes": "src/ProyectTypes",
"@utils": "src/Utils",
"@server": "src/Server",
"@prismaUtils": "prisma"
},
"_moduleAliases": {
"@controllers": "./build/src/Controllers",
"@dataDefault": "./build/src/Data",
"@middlewares": "./build/src/Middlewares",
"@routes": "./build/src/Routes",
"@proyectTypes": "./build/src/ProyectTypes",
"@utils": "./build/src/Utils",
"@server": "./build/src/Server",
"@prismaUtils": "./build/prisma"
}
From what we can see the root output with the tsc command is working fine but, when trying to run the application with npm start, the app presents the following error:
npm start
> [email protected] start
> npm run build && npm run start-app
> [email protected] build
> tsc
> [email protected] start-app
> cross-env NODE_ENV=production node -r module-alias/register build/src/index.js
node:internal/modules/cjs/loader:1080
throw err;
^
Error: Cannot find module './index'
Require stack:
- /var/www/proyects-Node/testProy/build/prisma/master/functions.js
- /var/www/proyects-Node/testProy/build/src/Utils/prisma/index.js
- /var/www/proyects-Node/testProy/build/src/Controllers/Master/UsersControllers.js
- /var/www/proyects-Node/testProy/build/src/Routes/Protected/MasterRouter/users/index.js
- /var/www/proyects-Node/testProy/build/src/Routes/Protected/MasterRouter/index.js
- /var/www/proyects-Node/testProy/build/src/Routes/Protected/index.js
- /var/www/proyects-Node/testProy/build/src/Server/index.js
- /var/www/proyects-Node/testProy/build/src/index.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
at Module._resolveFilename (/var/www/proyects-Node/testProy/node_modules/module-alias/index.js:49:29)
at Module._load (node:internal/modules/cjs/loader:922:27)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:121:18)
at Object.<anonymous> (/var/www/proyects-Node/testProy/build/prisma/master/functions.js:5:17)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/var/www/proyects-Node/testProy/build/prisma/master/functions.js',
'/var/www/proyects-Node/testProy/build/src/Utils/prisma/index.js',
'/var/www/proyects-Node/testProy/build/src/Controllers/Master/UsersControllers.js',
'/var/www/proyects-Node/testProy/build/src/Routes/Protected/MasterRouter/users/index.js',
'/var/www/proyects-Node/testProy/build/src/Routes/Protected/MasterRouter/index.js',
'/var/www/proyects-Node/testProy/build/src/Routes/Protected/index.js',
'/var/www/proyects-Node/testProy/build/src/Server/index.js',
'/var/www/proyects-Node/testProy/build/src/index.js'
]
}
and in each typescript file that uses the aliases, the following line of code is placed at the beginning of the file:
if (process.env.NODE_ENV === "production") import("module-alias/register");
As we can see I am trying to take the app to production but when the script is executed to enter production mode the app breaks and node says that it does not find the modules that are already in the subfolders of the build folder, which were generated by the npm run build command, if someone knows more or less why dende could address the problem would appreciate them.postscript, either try to delete the folder node_modules and package-lock.json and it did not work, and thoroughly check the paths and they are fine since I directly copy the error path in the terminal and using nano you can perfectly open the filem, then I do not know what it should be.