Require modules in function and functionitem [GOT CREATED]

It would be cool to be allowed to use node modules (native ones, at least) in Function and FunctionItem nodes.
This can be achieved replacing here for this (from vm2 readme):

const vm = new NodeVM({
    console: 'inherit',
    sandbox: {},
    require: {
        external: false,
        builtin: ['fs', 'https'], // this can also be ['*'] for all modules
        root: "./",
        mock: {
            fs: {
                readFileSync() { return 'Nice try!'; }
            }
        }
    }
});

I know it would not be safe so a workaround may be a new node called UnsafeFunction. Or maybe mock every dangerous function.

Hm doing that would kind of defeat the whole purpose why I used vm2 in the first place :wink:

Can however understand why you want to do that. What you could do is to just make the change to the node you want (and give it a different name) and then use the “n8n-node-dev” CLI like described here:

It will then build the node and copy it to the ~/.n8n/custom/ folder. Then simply restart n8n and your custom node will be loaded and you can use all the npm modules you want.

I followed your instructions to create a custom node so I could use the ‘http’ module in my functions. I keep on getting the error error TS1003: Identifier expected. when i try to build it.

My custom node code is the default code plus this line after the import statements
import * as http from 'http'.

Tried searching the web but couldn’t find anything, am I missing something here ?

Welcome to the community @kumar!

Please open a new issue. The problem you are having is not related to the Function or FunctionItem-Node. If we do not try our best to keep things on topic it will be very hard for people to find solutions to their problems. Thanks!

is this solved now? cause I have the very same requirement I guess. I want to use MomentJS in a FunctionItem node.

Is this possible?

You can do that. Check the link below.

1 Like

perfect, thanks!