Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency resolving problem with compiled byte code #159

Open
ishanuda opened this issue Oct 3, 2021 · 17 comments
Open

Dependency resolving problem with compiled byte code #159

ishanuda opened this issue Oct 3, 2021 · 17 comments

Comments

@ishanuda
Copy link

ishanuda commented Oct 3, 2021

Hi,
I tried the library with an app developed using the Nest.JS framework.
Library converted the bundle.js into bytecode and code ran without any errors.
NOTE: bundle.js was built following the bellow step 2.

However, I wanted to try with more advanced cases such as connecting to a database.

  1. I did setup the application to use sqlite3 along with TypeORM.

  2. Compiled the application into a single bundle using ncc library,
    NTOE: ncc library also copied the sqlite3 related node native module to the build folder successfully.

Now the library can run as a standalone file and tested and confirmed that I can do CURD operations with the database.

  1. After the successful test confirm I compiled the bundle.js with the bytenode and it could generate a compiled bytecode file.

When I run the code using bytenode ./build/bundle.jsc it throws the error of not resolving the ORM Repository.

I could not find any resources/ configurations to solve this issue.
Please help.

Resources:
@nestjs/core: ^8.0.0
sqlite3: ^5.0.2
typeorm: ^0.2.37
@vercel/ncc: ^0.31.1
bytenode: ^1.3.3
typescript: ^4.3.5

Thank you very much

@OsamaAbbas
Copy link
Collaborator

OsamaAbbas commented Oct 4, 2021

Could you please provide a complete minimal example that shows the problem with a simple npm install && npm run?

Also, try running the project like this: node -r bytenode ./build/bundle.jsc.

@ishanuda
Copy link
Author

ishanuda commented Oct 5, 2021

Hi @OsamaAbbas,
Thank you for the reply. I ran the suggested command and it still fails the operation.
However, I created a minimal repo in the following:

https://github.com/ishanuda/nestjs-ncc-bytenode

NOTE: I have update the README for your reference.

Thank you very much.

@OsamaAbbas
Copy link
Collaborator

Thank you for creating the minimal example. I apologize for the late response, and I will look into it in the weekend.

@ishanuda
Copy link
Author

ishanuda commented Oct 8, 2021

Thank you very much, I also updated the commands needed for your reference in case you needed.

@OsamaAbbas
Copy link
Collaborator

Hello again,

Neither his command node ncc/index.js nor npm run start:deb work with me. Both show multiple errors:

image

@ishanuda
Copy link
Author

ishanuda commented Oct 11, 2021

Hi,
Sorry for my bad,
There should be an env file that I ignored in the commit.
I have updated the env file so that the validation will pass while running.

NOTE: The .env file is mounted in app.module.ts with the following code.

envFilePath: path.resolve(__dirname, '../env', '.env'),

Thank you

@OsamaAbbas
Copy link
Collaborator

I can reproduce your error now. However, I don't even know how to approach the problem! There are many parts, each could potentially be the cause, and I have almost no experience with them.

I tried to google the error message. There are many people who had the same error but without bytenode. It was all related to nest configurations.

I believe that the way bytenode is wrapping your code inside this function (function (exports, require, module, __filename, __dirname) { YOUR_CODE \n}); plays a role. Maybe it causes some implicit globals to be lost. Maybe you should explicitly export what the error message is complaining about.

I'm sorry I could not help.

@ishanuda
Copy link
Author

ishanuda commented Oct 11, 2021

Hi,
Thank you for the response,
Yes, the dependency resolving problem is a common one if we did not inject a service before it is used in any other resources correctly.

The thing is, Before the bytecode conversion, the code runs well and it confirmed that the code has no errors.

As you suggest, may be the bytenode conversion effects this dependency injection and can not resolves the required services before it is used.

Generally, the TypeORM repository is working as a service and should import in the module before it is used by the service.
Ex: PostRepository (post.repository.ts) should import in the PostsModule (posts.module.ts) before using in the PostService (posts.service.ts).

As you can see the above PostRepository is already imported in the PostModule and can safely be used inside PostService by injecting the repository in the constructor with out the dependency resolving error.

I did not get the explicit export part you mentioned and if there is any hint that you can provided I can try with the code.

Library reference:
https://nestjs.com/
https://docs.nestjs.com/techniques/database
https://typeorm.io/#/

Thank you

@OsamaAbbas
Copy link
Collaborator

I did not get the explicit export part you mentioned and if there is any hint that you can provided I can try with the code.

Neither did I!

I tried one thing then I gave up. I started with adding PostRepository in the auth module imports. Then the error regarding PostRepository disappeared, but I got another error for UserRepository. So maybe you could try explicitly adding thing in the same way. I don't know the full structure of your project (and I have no experience with nestjs in general), so it was hard and time consuming to continue this route.

@ishanuda
Copy link
Author

Hi,
I will try to debug further and get back here soon with findings.
Thank you very much for your support and information

@ishanuda
Copy link
Author

Hi @OsamaAbbas,

I did import one module at a time in the app module and compiled the code and it works.
It seems that importing multiple modules in the app module create the problem.

As you can see in the image I commented the AuthModule and UserModule and compiled the code.
So now the code compiles and works perfectly fine.
Also, I tried with AuthModule and UserModule the same way and it works fine if only one module is imported.

If I try to import all at once the error is giving.

Do you have any idea or suggestions about this behavior?

image

Thank you very much.

@AwesomeSam9523
Copy link

@ishanuda don't forgot to export in compiled file as well

@ishanuda
Copy link
Author

Hi @AwesomeSam9523 ,
I am sorry I was away for a while, Thank you for the advice.
Could you please explain how to do it or modify the given example repo, so that I can further do the experiments and can help to close the issue or if I found any other issues that can bring here for further analysis.

Thank you very much

@AwesomeSam9523
Copy link

@ishanuda

require('bytenode');
module.exports = require('./filename.jsc');

@ishanuda
Copy link
Author

@AwesomeSam9523 ,
Thank you for the answer. Could you please tell me where to add this in the example to run the code.
I still did not get where to add this in the code.

Thank you very much.

@AwesomeSam9523
Copy link

@ishanuda Hey. I am not very sure about NEST Framework sorry. But what we do in JavaScript/electron is paste that in our entry file. Suppose there is a file main.js, which exports an Object.
When we compile main.js to main.jsc, we need to add module.exports = require('./filename.jsc'); instead of just require('./filename.jsc');
So when you later require it, it will return with the same Object you exported in original .js file.

I am sorry but not sure how NEST works. If this doesn't help you, please close the issue as I am not the right guy. I just sent that because I was doing same mistake so thought of telling you the solution.

@ishanuda
Copy link
Author

@AwesomeSam9523 Hi, No worries, I will try what you are suggesting here, But it was good to know this as well.
Thank you very much for the info anyway.
I am trying to run the code in NWJS :-)
Thank you very much once again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants