Npm dependencies in controllers

More and more applications are being developed on node, and i find it very easy to use npm libraries for both front and backend tasks. Since enonic xp controllers are written in javascript, it would be logical to be able to use already written javascript.
So i was wondering if there was any possibility to use node dependencies in an xp controller? Or if there was any plan to allow something like that?

Sounds like a job for PurpleJS.

Hi.

Yes, we do plan on having node dependencies (node_modules) in the dependencies of an xp app. It’s not done right now, but I know that @rfo have done something like this on Gjensidige.

1 Like

Hi.

I am working on an app that is using webpack and babel and allows npm modules to be included in controllers, tests and client scripts. It is not finished, but it is located here https://bitbucket.org/openxp/skyscraper. We are using externals in webpack to distinguish the enonic xp includes from the npm includes. I will add some more examples with using npm dependencies in controllers and tests the coming friday.

Examples (excerpts)

Code in controller:
const DomParser = require(‘dom-parser’);
const parser = new DomParser();

Code in package.json
“dependencies”: {
“dom-parser”: “0.1.5”
}

webpack.js
module.exports.site = () => {
return {
entry: getEntryFiles(’.es6’, [[‘main/resources/site’, ‘site’]],
output: {
path: ‘./build/resources/main/site/’,
filename: ‘[name].js’,
libraryTarget: ‘commonjs’
},
externals: [
//lib/(openxp|xp|enonic).+/
]
}

3 Likes