Webpack using forest-express-mongoose

Hello,

I am having trouble using webpack with forest-express-mongoose.
This line is causing my app to crash :

const forest = require('forest-express-mongoose');

Hello @Maxence_Fourrier,

Thanks for your message and welcome to our community! :raised_hands:

In order to help you, could you please tell me:

  • your project name
  • what is the exact error that you’re receiving?
  • did you install all packages properly (running yarn install or npm install)?

Thanks!

Hello,

My project name is : Avest

Webpack don’t gives usable errors. But here is what I get :

Error: existing class instance t in context
at t.value (app.bundle.js:2:496901)

Yes the package is installed.

I want to specify again that in my project I only have one line of code (the require of forest-express-mongoose)

What do you mean by that?
Did you generate your project using the forest-cli? Did you try to start it right away? Did it work?
Why would you have now only 1 line of code? :thinking:

Thanks.

I mean that my real project have more than one line, but for you to be able to reproduce the crash, I have create an other project with one line of code.

The project crash with webpack : https://webpack.js.org/ (Webpack is used by a lot of people to bundle JS projects)
The project works fine without webpack.

Thanks

@Maxence_Fourrier,

Sorry I think I misunderstood your issue in the first place.

You’re trying to add webpack on the project generated by the forest-cli, is that it?
So your project is working fine without it?
Could you detail a bit why you need to add it?

@adriguy ,

No, I am building my project using webpack for production.
In development, without building my project with webpack, the project works fine.
My problem occurred when I’m running the bundle given by webpack.

Hi @Maxence_Fourrier :wave:
First of all, Why do you use Webpack to build your Forest Admin app?

Webpack is mainly used to bundle frontend app, and forest-cli provides backend code that usually don’t need a build step to work.

Webpack can indeed be used to build NodeJS code, however, we’ve never tried to do so on our end.

I can’t think of specific reason that it wouldn’t work though.

In order to help, can you give me your Webpack config please? :pray:

1 Like

Hi,
Thanks for your feedback, I’m using webpack for mainly 3 reasons :

  • CI/CD: deployment of the production and development server is easier and cleaner with webpack. With webpack, we don’t need to specify witch file to send or not on our server when deploying, it import them automatically, it avoid useless files import on production server,
  • When deploying code on clients server, with webpack client cannot edit the code (webpack obfuscate the code),
  • Finally, we are using some packages that are transpile at runtime. With webpack, thoses packages are not transpile at runtime anymore.

My webpack configuration :

module.exports = {
    entry: {
        init: './script/init.js',
        main: './app.js',
        httpClientWorker: './src/cluster/httpClientWorker.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name].bundle.js'
    },
    target: 'node',
    mode: 'production'

Hey @Maxence_Fourrier :wave:

On my end, running with

app.js

const feMongoose = require('forest-express-mongoose');

webpack.config.js

const path = require("path");

module.exports = {
  entry: {
    main: "./app.js",
  },
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].bundle.js",
  },
  target: "node",
  mode: "production",
};

I have no issue at build time nor runtime. I did not try with both init and httpClientWorker since I’m not sure of what they contains. Of course, the forest admin agent does not work with such small content (Since express/feMongoose.init are not fully initialized).

However, and as stated by @Arnaud_Moncel, we do not test nor use webpack in a nodejs context on our end, so can’t be 100% sure that everything will work as expected with a webpack built version of our agent.

I’m assuming that once you remove the require('forest-express-mongoose') from your code and replace it by anything else (Like a console.log) everything works according to plan?
Could you share the webpack/webpack-dev-server version you are using, so I can also test on my end?

Thanks in advance

Hey @jeffladiray ,

Yes when I remove the “require” and replace it with a “console.log” everything works according to plan.
I’m using webpack version 5.64.4. Could you give me the version of webpack you were using too please ? Also I’m using node 16.13.0 and forest express mongoose 8.6.0

Thanks

Running on webpack@5.69.1, node@14.17.6, forest-express-mongoose@8.6.2 for my first tests.

Going to webpack@5.64.4, node@16.13.0, forest-express-mongoose@8.6.2 seems to give me the same result though :confused:
Did you try to comment both init and httpClientWorker to test if those can interfer with forest-express-mongoose?

Yes, I’ve try to remove both init & httpClient.
The error I’m getting : Send Anywhere

Hey @Maxence_Fourrier,

I can confirm that it comes from our dependency injection mecanism.
Adding a list of mangle reserved keyword allow me to build & run the mentionned code without issues.

  optimization: {
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          mangle: {
            reserved: [
              "logger",
              "pathService",
              "errorHandler",
              "ipWhitelist",
              "forestServerRequester",
              "schemasGenerator",
              "baseFilterParser",
              "permissionsFormatter",
              "ProjectDirectoryFinder",
              "ConfigStore",
              "PermissionsGetter",
              "PermissionsChecker",
              "ApimapFieldsFormater",
              "AuthorizationFinder",
              "ApimapSorter",
              "ApimapSender",
              "SchemaFileUpdater",
              "ScopeManager",
              "ModelsManager",
              "TokenService",
              "OidcConfigurationRetrieverService",
              "OidcClientManagerService",
              "AuthenticationService",
              "SmartActionFieldValidator",
              "SmartActionHookService",
              "SmartActionHookDeserializer",
            ],
          },
        },
      }),
    ],
  },

Far from being a perfect solution, but at least allow you to build your app without issue. I’ll let you know if I’m able to locate the exact issue and fix it without such configuration.

Hope this helps.

Hey @jeffladiray ,

Thanks, the require('forest-express-mongoose') is now working with the list of mangle reserved keyword using webpack.

I’m still getting error when running forest.init :

app.js

const mongoose = require('mongoose');

try {
    require('dotenv').config();

    const forestEM = require('forest-express-mongoose');

    if (forestEM && process.env.FOREST_ENV_SECRET
        && process.env.FOREST_AUTH_SECRET) forestEM.init({
            envSecret: process.env.FOREST_ENV_SECRET,
            authSecret: process.env.FOREST_AUTH_SECRET,
            objectMapping: mongoose,
            connections: { default: mongoose.connection },
        })
            .then((FAMiddleware) => {
                console.log('OK')
            })
            .catch((err) => {
                console.error('err', err)
            })
} catch (err) {
    console.error('failed to load forest');
}

The error

[forest] 🌳🌳🌳  An error occured while computing the Forest schema. Your application schema cannot be synchronized with Forest. Your admin panel might not reflect your application models definition. Cannot find module './forest'
{
  "code": "MODULE_NOT_FOUND",
  "stack": "Error: Cannot find module './forest'\n . . .
}

I’ve test the above code with valid models and a valid mongoose connection and I’m having the same error : Cannot find module ./forest

Are you able to replicate the issue ?

@Maxence_Fourrier

Usually, project using forest admin have forest directory (See below)

configDir represent the path to that folder, that is mandatory for Forest to run.The files within this folder are automatically loaded, but I’m pretty sure you should be able to make it run by simply creating an empty folder named forest and setting your configDir accordingly. Let me know if that helps

@jeffladiray ,
I’ve try to create this folder, I’m having the same error.

@Maxence_Fourrier,

Can’t reproduce on my end :confused:

As discussed, I’ve open an issue on our end and the original fix is in work in progress. I was hoping that my previous message could be enough to unlock the situation here, but it seems I was wrong.

I’ll prioritize the fix, and I’ll let you know once it is release (Instead of having to debug each issues you may encounter, since I can’t really reproduce the issue).

1 Like

@jeffladiray ,

Thanks, I’ll wait for the fix.

If you want help to be able to reproduce the error on your end, you can send me a google meet link and I’ll be glad to help you.