Share common code between App.js and Server.js

Hey all,

I was wondering if there is an easy way to share common code between App.js and Server.js.

I know I can do this via Public NPM packages, but the code is specific for our custom app so this would make it unnecessary complicated.

Cheers!
Jesper

Hey @JesperCorba You can create a local module within your project that both App.js and Server.js can require/import. You can structure your project with a folder for shared code, like /lib or /common, and place your shared JavaScript files there. Then, you can import these modules into both your server and client-side code.

// In /common/mySharedModule.js
module.exports = {
  sharedFunction: function() {
    // Shared code here
  }
};

// In App.js and Server.js
const mySharedModule = require('./common/mySharedModule');

Cheers!

Thank you very much. This helps a lot!

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.