I have an application which I deploy via github actions to heroku.
But it won't start, respectively it doesn't find the compiled sources to start the app.
Unfortunately I'm new to heroku and I can't find what might cause the issue.
Here is my tsconfig.json
{
"compilerOptions": {
"target": "es2018",
"module": "commonjs",
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"composite": true,
"experimentalDecorators": true
},
"exclude": [
"test"
]
}
my package.json has the following scripts (I only added the ones that are important to the problem)
"heroku-postbuild": "npm run compile",
"start": "node lib/",
"compile": "shx rm -rf lib/ && tsc"
heroku's ProcFile (autogenerated) shows the following
web npm start
(copied from dashboard)
the github action
- name: Deploy Backend
uses: AkhileshNS/heroku-deploy@v3.12.12
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "the name"
heroku_email: "example@mail.com"
appdir: "./server"
env:
HD_MONOGDB_CONNECTION: ${{ secrets.MONGODB_CONNECTION }}
HD_HOST: ${{ secrets.VUE_APP_BACKEND_URL }}
HD_PORT: ${{ secrets.BACKEND_PORT }}
HD_NODE_ENV: "production"
The deployment is ok, I can see the files with the heroku cli. According to heroku it also builds successfully.
But I see the following error in the logs
2022-04-12T10:06:46.016367+00:00 app[web.1]: Error: Cannot find module '/app/lib'
2022-04-12T10:06:46.016368+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
2022-04-12T10:06:46.016368+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:746:27)
2022-04-12T10:06:46.016369+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:75:12)
2022-04-12T10:06:46.016369+00:00 app[web.1]: at internal/main/run_main_module.js:17:47 {
2022-04-12T10:06:46.016370+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',
2022-04-12T10:06:46.016370+00:00 app[web.1]: requireStack: []
2022-04-12T10:06:46.016371+00:00 app[web.1]: }
Despite telling node via tsconfig to build/compile the code into ./lib
the folder doesn't exist.
It seems that the scripts are run from the correct folder in heroku (/app
) because I can find a tsconfig.tsbuildinfo
file
EDIT: Added build logs
-----> Building on the Heroku-20 stack
-----> Using buildpack: heroku/nodejs
-----> Node.js app detected
-----> Creating runtime environment
NPM_CONFIG_LOGLEVEL=error
NODE_VERBOSE=false
NODE_ENV=production
NODE_MODULES_CACHE=true
-----> Installing binaries
engines.node (package.json): ^14.0.0
engines.npm (package.json): >= 3.0.0
Resolving node version ^14.0.0...
Downloading and installing node 14.19.1...
Bootstrapping npm >= 3.0.0 (replacing 6.14.16)...
npm >= 3.0.0 installed
-----> Restoring cache
- node_modules
-----> Installing dependencies
Installing node modules
added 402 packages, and audited 403 packages in 10s
55 packages are looking for funding
run `npm fund` for details
3 high severity vulnerabilities
Some issues need review, and may require choosing
a different dependency.
Run `npm audit` for details.
-----> Build
Running heroku-postbuild
> @tasting-organizer/server@0.4.0 heroku-postbuild
> npm run compile
> @tasting-organizer/server@0.4.0 compile
> shx rm -rf lib/ && tsc
-----> Caching build
- node_modules
-----> Pruning devDependencies
up to date, audited 192 packages in 1s
13 packages are looking for funding
run `npm fund` for details
3 high severity vulnerabilities
Some issues need review, and may require choosing
a different dependency.
Run `npm audit` for details.
-----> Build succeeded!
-----> Discovering process types
Procfile declares types -> (none)
Default types for buildpack -> web
-----> Compressing...
Done: 34.4M
-----> Launching...
Released v7
https://tasting-organizer.herokuapp.com/ deployed to Heroku
If I run the scripts locally, everything works.
I just run the steps manually in heroku console.
Strangely tsc
doesn't emit anything but tsc --showConfig
shows the config I want.
I don't understand why it doesn't emit the files. I even added --noEmit false
to no avail.
Why are the compiled files not in /app/lib/
?
Any ideas?
I found the solution, although I'm not really sure as to why it is the solution.
./lib
to exclude
in tsconfig.json
"exclude": [
"test",
"lib"
]
lib
folder before building"compile": "shx rm -rf lib/ && tsc"
to "compile": "tsc"
Now it works.