Closed guilhermebkel closed 3 years ago
I have the same issue. My build scripts:
"scripts": {
"dev": "nodemon src/server.ts",
"start": "NODE_ENV=production pm2 start dist/server.js --name='api'",
"build": "rimraf dist && tsc -p tsconfig-build.json",
}
It works fine in development but it fails in production. Anyone know what might be going on?
I fixed it. I had this in my package.json:
"_moduleAliases": {
"@": "src"
}
I changed it to this:
"_moduleAliases": {
"@": "dist"
}
@randal923 Thanks for giving your approach for solving that! Unfortunately I already use that production config on my project and it does not work :/
@guilhermebkel I see. I will share all my configs with you. If you still can't get it to work, message me on Discord at shui#8603 and we can figure it out.
So, I am import in my the module-alias/register in my server.ts:
import 'module-alias/register'
import * as dotenv from 'dotenv'
import App from '@/app'
dotenv.config()
const app = new App(Number(process.env.PORT))
app.listen()
If you are using TypeScript, I have this in my TS config:
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2019",
"esModuleInterop": true,
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"baseUrl": "./src",
"paths": {
"@/*": [
"*"
]
}
},
"include": [
"src"
],
"exclude": []
}
Lastly, I have the module alias in my package.json like I have shown above:
"_moduleAliases": {
"@": "dist"
}
I believe this is it. It was super straight forward. If I had to guess what is wrong with yours is you are not importing the module-alias/register before running build. You have to import it the main file of your app, before any other code.
@guilhermebkel I think may be an issue in how this module interacts with yarn workspaces. For example, if you put your _moduleAliases
in your root package.json (adjusting the relative paths), it works.
Hello guys! Thanks for giving a feedback about my error!
At first, @randal923 I had already used these configurations and it didn't work š, I would like to give you a huge thanks for offering a help on discord to me, but before I tried to get in touch with you, I've tried the tip that @danielrhodes has given in the last comment and it worked like a charm! It really looks like an issue of this module behavior when dealing with workspaces (so adding the module alias config in the root package.json really works).
A huge thanks for all the help!
any solution for this? I'm using module-alias for a npm package I created but when I'm trying it after install it I get this error:
Error: Cannot find module '@root/inspector'
Seems to not recognize custom paths.
Hello! Recently I've started being in touch with a strange bug. It seems that the module-alias does not work on my production environment (does not matter if I run the same commands to build and start the app, it only works on development environment but not on production) š¤
I'm currently using lerna to run all project commands because it is a project inside a monorepository. So, in practice I build the app using the following Dockerfile:
So, after building this app, I try to run it with the following command:
Then, I receive the following logs depending on the environment I'm currently in:
lerna notice cli v3.22.1 lerna notice filter including "@uno-game/unapy" lerna info filter [ '@uno-game/unapy' ] lerna info Executing command in 1 package: "npm run start"
lerna notice cli v3.22.1 lerna notice filter including "@uno-game/unapy" lerna info filter [ '@uno-game/unapy' ] lerna info Executing command in 1 package: "npm run start" @uno-game/unapy: > @uno-game/unapy@0.0.0 start /ssd/Github/uno-game/packages/unapy @uno-game/unapy: > node -r module-alias/register dist/index.js @uno-game/unapy: Server is running... [PORT 5000]