Hi Forge Team,
Recently I was running into some issues where I couldn’t bundle my Forge app when deploying or tunnelling. The error I got was:
Error: Bundling failed: Module not found: Error: Can't resolve 'package-name' in '/forge-app-directory/src'
[stack trace]
After patching @forge/bundler
to spit out webpack stats
, there was additional debugging information from the failed resolution. Perhaps the error handling could additionally include errors.details
from Webpack in @forge/bundler
rather than only a stack trace from the CLI.
"errors": [
{
"moduleIdentifier": "/Users/skooch/.nvm/versions/node/v18.16.1/lib/node_modules/@forge/cli/node_modules/babel-loader/lib/index.js??ruleSet[1].rules[0].use[0]!/Users/skooch/projects/forge-app-directory/src/index.js",
"moduleName": "./src/index.js",
"loc": "5:0-29",
"message": "Module not found: Error: Can't resolve 'package-name' in '/Users/skooch/projects/forge-app-directory/src'",
"moduleId": 4066,
"moduleTrace": [],
"details": "
resolve 'package-name' in '/Users/skooch/projects/forge-app-directory/src'
Parsed request is a module
using description file: /Users/skooch/projects/forge-app-directory/package.json (relative path: ./src)
Field 'browser' doesn't contain a valid alias configuration
resolve as module
/Users/skooch/projects/forge-app-directory/src/node_modules doesn't exist or is not a directory
looking for modules in /Users/skooch/projects/forge-app-directory/node_modules
single file module
using description file: /Users/skooch/projects/forge-app-directory/package.json (relative path: ./node_modules/package-name)
no extension
Field 'browser' doesn't contain a valid alias configuration
/Users/skooch/projects/forge-app-directory/node_modules/package-name is not a file
.ts
Field 'browser' doesn't contain a valid alias configuration
/Users/skooch/projects/forge-app-directory/node_modules/package-name.ts doesn't exist
.tsx
Field 'browser' doesn't contain a valid alias configuration
/Users/skooch/projects/forge-app-directory/node_modules/package-name.tsx doesn't exist
.js
Field 'browser' doesn't contain a valid alias configuration
/Users/skooch/projects/forge-app-directory/node_modules/package-name.js doesn't exist
.jsx
Field 'browser' doesn't contain a valid alias configuration
/Users/skooch/projects/forge-app-directory/node_modules/package-name.jsx doesn't exist
.json
Field 'browser' doesn't contain a valid alias configuration
/Users/skooch/projects/forge-app-directory/node_modules/package-name.json doesn't exist
Knowing that other packages were still bundling fine. I decided to review the webpack configurations generation code in the config/
directory, I discovered this line here in config/sandbox.js:L102
:
webpackConfig.resolve = Object.assign(Object.assign({}, webpackConfig.resolve), { mainFields: ['main', 'module'], exportsFields: [], alias: getCustomModulesAliases() });
Clearly, it makes sense to override the resolve configuration so you can ensure your aliases apply. However while mainFields
was set to the default, exportsFields
was not, meaning that Webpack would ignore that field in a package.json
.
I checked my installed package and sure enough, the package.json
was using exports
to specify the entrypoint, not main
or module
.
[...]
"type": "module",
"exports": "./dist/index.js",
[...]
This is not against the spec - but it is only supported in Node 12 and above. I would assume that Forge does not use any version older than that given the engines specification in most of the Forge packages is >=12.13.1
.
I patched config/sandbox.js:L102
in @forge/bundler@4.10.2
to:
webpackConfig.resolve = Object.assign(Object.assign({}, webpackConfig.resolve), { mainFields: ['main', 'module'], exportsFields: ['exports'], alias: getCustomModulesAliases() });
And it all started to work just fine - with the exception of forge tunnel
because I’d have to patch the dependency in the container. So I’ve also patched my dependency’s package.json to add a main
field.
I’ve checked to see if @forge/bundler@4.10.3-next.15
has this issue fixed, and I couldn’t see anything but dependency updates in the changelog.
Here’s an npm why
output so you can see I’m using the latest version of said package:
âžś forge-app-directory git:(develop) âś— npm why @forge/bundler
@forge/bundler@4.10.2 dev
node_modules/@forge/cli/node_modules/@forge/bundler
@forge/bundler@"4.10.2" from @forge/cli@6.14.1
node_modules/@forge/cli
dev @forge/cli@"^6.14.1" from the root project
@forge/bundler@"4.10.2" from @forge/tunnel@3.6.2
node_modules/@forge/cli/node_modules/@forge/tunnel
@forge/tunnel@"3.6.2" from @forge/cli@6.14.1
node_modules/@forge/cli
dev @forge/cli@"^6.14.1" from the root project
I’m using Node v18.16.1 (npm v9.5.1)
I haven’t checked for any regression on this issue, so you may want to do that and backport this fix as required. I couldn’t find a repository for this package on Bitbucket or GitHub to submit a PR against, otherwise I would have done that.
Hope this helps all who may have had packaging issues while using Forge! Looking forward to getting the fix in!