pkg icon indicating copy to clipboard operation
pkg copied to clipboard

when pkg is run, i keep getting errors about dynamic libraries

Open ambrighter opened this issue 8 years ago • 9 comments

node version 8.11.0 npm version 6.4.1

I keep getting an error that says:

[debug] Cannot resolve ''./nconf/stores/' + store' C:\Program Files (x86)*\workspace*\node_modules\nconf\lib\nconf.js Dynamic require may fail at run time, because the requested file is unknown at compilation time and not included into executable. Use a string literal as an argument for 'require', or leave it as is and specify the resolved file name in 'scripts' option.

this happens for multiple node-modules and when it finally builds the exe, it fails when it tries to call nconf.argv saying that it was not included in the build.

Ive tried multiple things over the past few days, doing complete rebuilds, clearing caches, reimporting the project and i keep running into the same issue.

Any ideas on where to begin, or where to go with this?

ambrighter avatar Oct 26 '18 00:10 ambrighter

You can start using a require.js file to add all the dynamically required files in there and add the file to configuration in package.json.

https://github.com/zeit/pkg#config

this fixed all my problems with dynamic require.

sharathbaddam avatar Oct 30 '18 14:10 sharathbaddam

@sharathbaddam thanks for your response.

i havent been able to find any examples that use a requrire.js file or how it is supposed to be organized.

i am assuming that it just has all the specific requires and then is somehow brought into the pkg part of the package.json?

ambrighter avatar Nov 01 '18 16:11 ambrighter

@ambrighter Yes, basically you create a file (require.js - for example) and add all your dynamic requires from the warnings you get when building an executable using pkg as shown below :

require('./node_modules/nconf/lib/nconf.js); require('./node_modules/some_module/lib/someFile.js); require('./someLocalFile.js');

Later, add this file name in package.json as shown below : "pkg": { "scripts": "require.js" }

Hope that helps !!

sharathbaddam avatar Nov 02 '18 16:11 sharathbaddam

Thanks, @sharathbaddam. I was running into the same "Cannot resolve" issue that @ambrighter was seeing and your solution worked for me. Thanks!

By the way, do you know if the name of the file with the require calls matters? I didn't do much testing, when I changed the name of the require.js file to something else (both the file and in package.json) Pkg seemed to fail to pull in the items in the require file. Once I changed it back to require.js things worked as expected.

dwhiting10 avatar Jan 18 '19 20:01 dwhiting10

But what if I don't know what will the resolved file name be? Like for example I create temporary file with name according to some arbitrary variable that the app holds in code.

Perhaps an option to let the app require dynamic files outside of the binary so that it's not needed to resolve them during compilation could prove useful to some people.

Edit: Nevermind, ignore that. I didn't notice it's already possible. It is documented how to achieve it over here and as long as the file is actually there on the user's filesystem, the warning can be safely ignored.

miloslavnosek avatar May 11 '19 12:05 miloslavnosek

Hey @sharathbaddam , could you have a look into #831?

I'm having a very similar issue and maybe you can help me out there.

Thanks!

eduardoewgo avatar Jan 20 '20 18:01 eduardoewgo

This issue is stale because it has been open 90 days with no activity. Remove the stale label or comment or this will be closed in 5 days. To ignore this issue entirely you can add the no-stale label

github-actions[bot] avatar Apr 09 '21 00:04 github-actions[bot]

Dynamic required packages cannot be resolved automatically by pkg, you should place them on scripts in package.json pkg config

robertsLando avatar Apr 09 '21 06:04 robertsLando

@ambrighter Yes, basically you create a file (require.js - for example) and add all your dynamic requires from the warnings you get when building an executable using pkg as shown below :

require('./node_modules/nconf/lib/nconf.js); require('./node_modules/some_module/lib/someFile.js); require('./someLocalFile.js');

Later, add this file name in package.json as shown below : "pkg": { "scripts": "require.js" }

Hope that helps !!

Do you mean we add the modules we use in our script into a created file call require.js and add "pkg": { "scripts": "require.js" } inside the dependencies inside the package.json?

xuakson avatar Jan 15 '23 22:01 xuakson