Forge bundling fails on js language feature

I am using an npm package in my forge server code which uses the private fields feature of js. I find that private fields work fine when used directly in my application classes however the bundler seems to choke on their use in an imported package, complaining specifically about the line where the private field is declared (it doesn’t seem to recognize the syntax of this feature - introduced in node 12.0.0). Here is the error I am seeing:

=== Bundling code...

Error: Bundling failed: ./node_modules/aggregate-error/index.js 7:1
Module parse failed: Unexpected character '#' (7:1)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| 
| export default class AggregateError extends Error {
>       #errors;
| 
|       name = 'AggregateError';
 @ ./node_modules/p-map/index.js 1:0-45 61:17-31
 @ ./src/customFieldSupport.js
 @ ./src/customUI.js
 @ ./src/index.js

The error hints that I may need to use a special loader. Is there a trick to handle this?

1 Like

Hi @jeffryan,

Thanks for raising this issue. I’ll look into this and get back to you.

Hey @jeffryan,

Unfortunately, because most NPM packages come with a pre-compiled dist, the webpack config we use to bundle the Forge app avoids compiling the node_modules, which ends up breaking the bundling step if there is syntax it doesn’t understand (i.e. new syntax which is uncompiled).

We can include node_modules in our bundler’s webpack config, but this greatly increases the time taken to bundle the app. I will add making this an option at least to our team’s backlog.

In the meantime, there are a couple ways to work around this.

Probably the easiest option, but quite hacky and not easily saveable/shareable is to update your Forge CLI source code to remove exclude: /node_modules/ from the webpack config we use for bundling. You’ll need to find the source code of the CLI using which forge to find the executable and then follow the symlinked executable to the source code. In the source code, it will have some node-modules, one of which is @forge/bundler and in there you can go to out/webpack.js and delete the line: exclude: /node_modules/, under test: /\.jsx?$/,. Then you should be able to use forge deploy and even forge tunnel without the error happening. Obviously this is far from ideal, just a suggestion if you don’t want to wait for us to make the appropriate change on our side.

Another possible option is to add your own bundling that turns your code (including node_modules) into a JS file that our bundler can understand and doesn’t need to do anything fancy with.

2 Likes

Thanks for the response @kchan. For the moment I’ve backed off to an earlier version of the package that was giving me problems. They only recently changed to use private fields and the earlier version works for me. This is ok short term but longer term I’ll need to consider your other options. I appreciate you looking into it for me and giving me some workarounds.
Jeff

2 Likes