[HELP] Not been able to import external modules

Hi,
I have been trying to install external modules like ‘moment’, ‘lodash’ locally on my n8n dashboard. I have installed it in my n8n/node_modules folder. When I try to ‘require’ it in my function node it gives me error as “cannot find module ‘lodash’”. I have followed the steps mentioned at https://docs.n8n.io/#/configuration by setting the environment variables as export NODE_FUNCTION_ALLOW_EXTERNAL=lodash but still doesn’t work. I am looking for a way to make it happen I’d really appreciate a quick response. Thank you in advance.

Sorry, do not understand where you want to use this modules? If you say in your “n8n dashboard” you mean you are changing the actual UI? Or do you mean you want to use it in a Fuction-Node?

No I am not modifying any UI. I want to use it inside Function-node just like we’d do in any Javascript file. I’ve tried writing this simple line of code and it doesn’t seem to be working.

@chinmay_chavan working on a node that format dates so that you do not have to import any packages. Is that the use case you are looking for?

1 Like

No not really, that was just trial and error. For my use case I want to import built.io package from npmjs.com.

@chinmay_chavan did play around with it two days ago and now today again. Sadly does now work like expected. It works fine in the dev-environment but not when installed regularly. Hope I can figure out the problem soon.

1 Like

Ok did figure out the issue. I did actually already find it two days ago but never worked in my tests anyway (which confused me a lot). I just found out why. I always changed the code in the Function-Node but my test workflow had the FunctionItem-Node. Totally stupid!!! Hours waisted for nothing. Anway is fixed now and will be released with the next version.

1 Like

Ok released now [email protected] with the fix.

Just tested like that and it works now fine:

# Go in folder you want to install n8n in and install n8n with the modules you need
npm install n8n moment
# Allow access to the modules
export NODE_FUNCTION_ALLOW_EXTERNAL=moment
# Start n8n
node node_modules/.bin/n8n
1 Like

Could you suggest a way I can make this work with the Docker container, please? Or, will this only work with standalone installs?

Haha! We’ve all been there mate! Thank you for taking out the time to work on this and making it work! :slight_smile: Kudos!

I’ll try this out and get back here, Thanks once again!

It is theoretically also possible with the docker image but much more messy as you have to mount each module you need separate. For “sugar-date” that would for example additionally be “sugar-core” as “sugar-'date” depends on it. The more complex the module you need the more modules that would be. So for it to work you would have to:

  1. npm install “sugar-date” (or whatever you want) in a folder (in this example it would be ~/whereever-you-have-the-module)
  2. Then start the n8n container kind of like this:
docker run -it --rm \
  --name n8n \
  -p 5678:5678 \
  -e NODE_FUNCTION_ALLOW_EXTERNAL=sugar-date \
  -v ~/.n8n:/root/.n8n \
  -v ~/whereever-you-have-the-module/node_modules/sugar-core:/usr/local/lib/node_modules/sugar-core \
  -v ~/whereever-you-have-the-module/node_modules/sugar-date:/usr/local/lib/node_modules/sugar-date \
  n8nio/n8n:0.51.0

That worked perfectly! Thanks a lot, @jan. :slight_smile:
Will stick to the Docker container for now and then move on to a native install if I end up needing lots of such modules.

Great to hear that it worked! Wish you a great day!

1 Like

For anyone wondering how to allow multiple, just put them in a comma separated list like so

export NODE_FUNCTION_ALLOW_EXTERNAL=moment,date-fns,feiertagejs

(as a multiple export of the single value will obviously override the previous one)
Great feature, thanks

EDIT:
For those who want to use date-fns and date-fns-tz (tz stands for timezone), in order to have it fully supported ( full-icu)you need nodejs > 13. I ran into an issue:

The module ‘/var/www/vhosts/rz.gl/mat.rz.gl/node_modules/mmmagic/build/Release/magic.node’
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72.

I solved it by first updating nodejs and npm to latest version and then running

npm rebuild mmmagic--update-binary
1 Like