Closed ItsInsajd closed 4 years ago
Hi @ItsInsajd , thanks for the feedback, it is a great job that you have performed. This is a beta version yet, I have tested a lot of use cases, so it is very useful to receive feedbacks. So, let's on the analysis:
The if code:
const out = 'dist';
if (!fs.existsSync(out)) {
fs.mkdirSync(out);
fs.mkdirSync(out + '/config');
}
I understand now that it fails in case that the dist folder exist, but it does not contains config folder... So we have to check if exist dist/config path and create it.
To run directly javascript build app, you have to execute:
node dist/main.js
Or
npm run start:prod
Please check the README generated under server part
const yamlConfig = yaml.safeLoad(fs.readFileSync(__dirname + '/../../config/application.yml', 'utf8'));
const envYamlConfig = yaml.safeLoad(fs.readFileSync(__dirname + `/../../config/application-${process.env.NODE_ENV}.yml`, 'utf8'));
So I have to fix that scenario.
For the env variable, I notice that in some machine they are not read. However, you can execute a npm/node task passing it. For example:
# run production build with node
$ set NODE_ENV=prod&& node dist/main.js
You can always check it in generated server README
Hey, thanks for response. In my case, node dist/main.js
or npm run start:prod
won't work, because there is no main.js file in dist folder. When running npm run build
, my dist folder is as follows:
dist/
----config/
----scripts/
----src/
So running node dist/main.js
will fail.
I also tried reverting copy-resource.ts to default state (before my change described in first post). Running npm run build
then shows the same error as earlier: cp: dest is not a directory (too many sources)
.
So I modified copy-resources.ts again. Calling set NODE_ENV=prod&& npm run build && npm run start:prod
causes copy-resource script to run two times, so I decided to check if config folder exists to avoid another error (Error: EEXIST: file already exists, mkdir 'dist/config'
).
const out = 'dist';
if (!fs.existsSync(out)) {
fs.mkdirSync(out);
}
if (!fs.existsSync(out + '/config')) {
fs.mkdirSync(out + '/config');
}
Buuuuut this still fails, because there is no main.js in dist/
. In my case, main.js lies inside dist/src/. And the same goes for node dist/main.js
.
Hi, try these steps:
set NODE_ENV=prod&& npm run build && npm run start:prod
After this you can run npm run start:prod
because you have the javascript build.
You can refer to the README. generated. The start:prod task is not allowed without npm build.
Besides, for the env variables the generated project uses those defined in the .env file. Maybe in Azure or in the cloud that is ignored and you have to pass in the npm task (set NODE_ENV=prod&& npm ...
). So the env variables are not in clear.
However, I have to fix:
copy-resources.js
to copy, generate and read config.Thanks a lot for the feedbacks.
Hey, to clarify, I ran npm run build
before npm run start:prod
. Creating /scripts
folder inside dist/
also doesn't seem to make any difference. The main problem is that main.js
file is not in dist
folder, it's inside dist/src/
, where other built source files are. I don't know why this is the case. I ran set NODE_ENV=prod && npm run build && npm run start:prod
on my friend's PC with the same setup as described in first post and the result is the same as mine.
If i understand you correctly, after build the main.js file should be in dist/
path? Can you show me screenshot of your dist/
directory?
Anyways, it seems to work with node dist/src/main.js
so this is not the biggest problem. I will try to dig a bit more if I have time for it, maybe running on my home laptop, where I have different configuration. For now, I have no clue what's wrong.
Thanks for your time :)
@ItsInsajd you are welcome! feel free to say what you want :) So, let me explain what should be the normale behaviour:
I'm checking on resolve-env branch
@ItsInsajd , you can dowload that branch and check if is all ok?
Follow the steps: https://github.com/jhipster/generator-jhipster-nodejs/tree/bug/resolve-env#-steps-to-develop-a-generator-feature-and-test-it
However, the right paths, that I have fixed, of the main build under server is dist/main.js . I have added also integration test for this: https://github.com/jhipster/generator-jhipster-nodejs/runs/423982763
Besides I'm adding a feature with webpack for a full bundle.js that not require node_modules folder.
Fix with #102 , I will release the 1.0.0-beta.3
Hi @ItsInsajd, you can test the new version just published 1.0.0-beta.3. Feel free to open new issues if there are another problems. Thanks!
Hey, sorry for late update, but it seems that indeed problems listed in this issue have been resolved. I can now run set NODE_ENV=prod&& npm run build && npm run start:prod
and the application builds and runs as expected. Thanks for your work!
Hi @ItsInsajd , no problem for the delay.. you are welcome! I'ts my pleasure to grow up the community around this project. Thanks you a lot!
Describe the bug I wanted to check how reliable is to use nodejs blueprint to kickstart a project with azure deployment. Commands used for prod build do not work as excepted, but I may be doing something wrong, so I decided to ask here.
To Reproduce Run blueprint as monolith, server only. Database probably doesn't matter, but i selected postgres. Then run
npm install
in main dir and in /server dir. Next:cd server
npm run build
First problem: After running
npm run build
, it logs an error when running copy-resources script:After a bit of digging, it turns out an error lies in this part in copy-resources.js
I think because dist folder already exists after build,
/config
folder cannot be created and files cannot be copied properly. Movingfs.mkdirSync(out + '/config');
outside of thatif
fixes this problem.Second problem: If then you try to use
node disc/src/main.js
to run the app, next problem arises.If you inspect the path where application.yml file should be, you'll notice it tries to find it inside dist/src/config/ file. But this file is inside
dist/scripts
, so this path is wrong. I fixed it by changing paths in config file:Third problem: For some reason, env variables are not resolved on azure. For example, process.env.NODE_ENV is undefined and i had to hardcode 'prod' in this line
to make it work. NODE_ENV should come from .env file, but it's not resolved.
Expected behavior App builds and starts as expected.
Desktop (please complete the following information):
Additional context After one day of work, we managed to finally run it on azure, but env variables are in some parts hardcoded, which is far from ideal.