How to add node modules in Javascript action code

0
Hi everyone, I want to write a javascript action with some node modules functionalities. In pure Node js code we can install node modules by “npm install XXXX” , then we can use them in a js file by import . The same thing I want to do in our javascript action . The following steps i have done. I have opened Project Folder/javascriptsource/<my_module_folder>/actions folder. then installed one module let say “fs”.   Created package.json file  and updated dependency  Written code in Javasctipt action { "requires": true, "lockfileVersion": 1, "dependencies": { "fs": "0.0.1-security" } } import { Big } from "big.js"; // BEGIN EXTRA CODE // END EXTRA CODE /** * @returns {Promise.<string>} */ export async function JavaScript_action() { // BEGIN USER CODE const fs = require("fs"); fs.mkdir("D:/folder1", { recursive: true }, function(err) { if (err) { console.log(err) } else { console.log("New directory successfully created.") } }); return Promise.resolve(); // END USER CODE } when I run locally it throws error as fs not defined. Anyone can u pls tell me what i made wrong. How we can use node modules in our javascript action. because I need to add few more node modules in my javascript action. Please suggest me.     Thanks, Vijayakumar.
asked
2 answers
1

Have you seen this (brief) example in the documentation?

https://docs.mendix.com/howto/extensibility/best-practices-javascript-actions#2-2-6-using-external-dependencies-in-the-browser

Also see this example from the official Mendix repo of how react-native-geolocation is used for native apps.

If that’s not working for your use case, update your question with the actual library you’re trying to use, and perhaps we can help.

answered
0

You’re doing things mostly right. However, two issues I see:

  • You can use regular import statements in the EXTRA CODE section, though require should work
  • fs is not meaningful/useful in the context of a JS action. What are you going to do on the file system from a browser? See the npm docs: https://www.npmjs.com/package/fs
answered