I want to use pkg to bundle one of my packages in a monorepo, using yarn workspaces
repository: https://github.com/entria/entria-fullstack
command run:
pkg packages/server/src/index.js
this is the error:
Warning Cannot find module './app' from '/entria-fullstack/packages/server/src'
Could be ./app is a TypeScript file and therefore not parsable by pkg as it does not handle transpiling for you. Instead of running pkg with a path as it's argument, try adding the entrypoint path for pkg in your package.json under the bin key (Refer to README).
Then, from your project root run pkg package.json. If still you hit a wall, post the output when running the previous CMD with the --debug flag?
I'm trying to get this to work in a monorepo also. https://github.com/intuit/auto/tree/v7
In my case i have a cli tool and a bunch of plugins. the cli tool doesn't depend on most of the plugins but i would like to bundle those into the package as well. Right now whenever i try to load a plugin it cannot find the package name. This probably falls under the dynamic require situation, but I've tried both assets and scripts to no avail
@hipstersmoothie did you pass the right directory of the plugins?
maybe you have yarn workspaces enabled in which case you need to include those packages from parent directory node_modules
I have the following directory structure with yarn workspaces:
- node_modules
- packages
| |- shared
| | |- package.json / index.js / etc
| |- core
| | |- package.json / index.js / etc
| | |- node_modules
| | | |- .bin
|- ...
in packages/core/package.json I have to include packages from ../../node_modules in my assets setting, else they won't get included because local node_modules folder is empty except for .bin folder.
{
"pkg": {
"targets": [
"node12"
],
"scripts": [
"package.json",
"cli/commands/*.js",
"../../node_modules/some-module/dependency/**/*.*"
],
"assets": [
"public/**/*.*",
"..."
]
}
Yeah that what i'm finding too. This really isn't tenable cause my bundle will break everytime a dep is added to a package.
It must not be doing normal node_module resolutions (walking up directories). this seems like a bug to me
If I include all the node modules my code works but the bundle is huuuuuuge
{
"pkg": {
"assets": [
"./node_modules/**/*",
"./plugins/**/*"
]
},
}
@igorklopov
What do you mean by
it must not be doing normal node_modules resolutions (walking upd directories)
Do you mean the node_modules won't get found or it isn't feasible because of restrictions of your workspace?
You shouldn't include the whole node_modules folder, just add the packages that need this special inclusion.
What I can think of is that you create a script that adds modules if you run yarn add or npm install --save to that array which you can remove if it works without it.
scriptsscriptsDo you mean the node_modules won't get found or it isn't feasible because of restrictions of your workspace?
no i mean this: Given you above example when you run the code normally in node. the require will check the local node_modules then walk up the directory to the root node_modules. This is how you package functions normally. What pkg seems to be doing is only checking the local node_modules instead of walking up to the root one.
As for your other points that would probably work but it seems like a lot of hassle for what this tool should probably be doing in the first place
I am assuming that it bundles based on require statements too, but i could be wrong
I just got it working!
Initially I was putting my plugins in assets so the package.json's were included and I could just try to require the directory. This skipped the important step of compiling the code with v8. This is what scripts is for.
I changed my config to:
{
"pkg": {
"scripts": "./plugins/**/dist/**/*.js"
}
}
and where i try to load the plugin I use __dirname and the path to the plugin's main
// for bundle
if (!plugin) {
plugin = tryRequire(
path.join(
__dirname,
'../../../../../plugins/',
pluginPath,
'dist/index.js'
)
) as IPluginConstructor;
}
Most helpful comment
@hipstersmoothie did you pass the right directory of the plugins?
maybe you have yarn workspaces enabled in which case you need to include those packages from parent directory
node_modulesI have the following directory structure with yarn workspaces:
in
packages/core/package.jsonI have to include packages from../../node_modulesin myassetssetting, else they won't get included because localnode_modulesfolder is empty except for.binfolder.