Help with 'import' of NPM module for use with a custom Wappler Extension

Hey All,

I’ve got some custom JS that i’ve built for an app and it is all working fine except i can’t get it to work now that i’ve finished testing it and now trying to set it up as a Wappler extension.

I’ve used and built some extensions before so i can usually get them to work, the problem this time is i’m making use of the ‘regression.js’ module (https://www.npmjs.com/package/regression) and it seems to be built differently to other modules i’ve used in the past successfully (Nanoid and Lodash primarily)

The instructions of regression talk about loading it using import whereas all the other modules i’ve used use the require command.

I can’t use import in the wappler extension and i’ve tried using require but it can’t locate the regression module and throws an error like:

"ReferenceError: regression is not defined\n    at Object.getAutoRegression (/opt/node_app/extensions/server_connect/modules/forecast_engine.js:400:31)\n    at /opt/node_app/extensions/server_connect/modules/forecast_engine.js:372:65\n    at Array.forEach (<anonymous>)\n    at Object.getRegressionData (/opt/node_app/extensions/server_connect/modules/forecast_engine.js:360:18)\n    at forecastCalls (/opt/node_app/extensions/server_connect/modules/forecast_engine.js:828:48)\n    at /opt/node_app/extensions/server_connect/modules/forecast_engine.js:1106:9\n    at Array.forEach (<anonymous>)\n    at forecast_engine (/opt/node_app/extensions/server_connect/modules/forecast_engine.js:566:20)\n    at App.exports.forecast_engine (/opt/node_app/extensions/server_connect/modules/forecast_engine.js:1117:12)\n    at App._exec (/opt/node_app/lib/core/app.js:485:57)"

I’ve tried to write the require statement like:
require('regression') or const regression = require('regression')

but still can’t get it to work. I think the regression module isn’t packaged up to use require() ?? although my understanding of this part of JS and Node is limited.

For instance, in the regression folder in node_modules, it doesn’t’ have an index.js file like other modules i’ve worked with?

I wonder if its just a matter of specifying the path differently in the require statement?

Any thoughts would be much appreciated…apologises for the rambling :slight_smile:

Why not?

(genuine question)

It could be a user error, but it throws an error when i try and use the recommended import statment at the top of the extension file.

import regression from 'regression';

gets error 500

"Cannot use import statement outside a module"

Did a small test with regression and had no problems with requiring it.

You can’t combine import and require in a project, Wappler uses commonjs modules with require and import statements will fail.

const regression = require('regression')

worked for me

Thanks Patrick, appreciate you testing this. Good to know it works, clearly something wrong on my end.

I’ll figure it out :slightly_smiling_face:

If anyone else reads this, i was able to get this to work, but after installing the relevant node module (regression.js in my case) i had to re ‘deploy’ the project so that it re-built all the node modules etc.

This seemed to fix it and it now works.

Thanks @patrick for confirming it worked.