# Code node, cannot use external npm module

**URL:** <https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097>\
**Category:** Questions\
**Tags:** npm\
**Created:** [January 29, 2024, 11:55pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097 "2024-01-29T23:55:09Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sengerio](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/sengerio/32/18071_2.png) [@Sengerio](https://community.n8n.io/u/Sengerio)\
**Post date:** [January 29, 2024, 11:55pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/1 "2024-01-29T23:55:09Z")

</div>

## Describe the problem/error/question

Hi. I’m using n8n self hosted on a Docker environment, the n8n version is 1.25.1  
I have noticed that from about two weeks my Function Node has ceased to function properly.  
I’m using the Function Node to make some tests using the chai library.

At the start of the code, i require the library:  
“var assert = require(‘chai’).assert;”  
But i got the following error:  
“Error [ERR\_REQUIRE\_ESM]: require() of ES Module /usr/local/lib/node\_modules/chai/chai.js from /usr/local/lib/node\_modules/n8n/node\_modules/@n8n/vm2/lib/resolver-compat.js not supported.  
Instead change the require of chai.js in /usr/local/lib/node\_modules/n8n/node\_modules/@n8n/vm2/lib/resolver-compat.js to a dynamic import() which is available in all CommonJS modules.”

I’m using the chai library found here → [GitHub - chaijs/chai: BDD / TDD assertion framework for node.js and the browser that can be paired with any testing framework. · GitHub](https://github.com/chaijs/chai)  
I’ve searched and tried any sort of things/workarounds to make it work, but nothing seems to function. Unfortunately i cannot say exactly when it ceased to function.

The only configuration i have is the ENV variable:  
“ENV NODE\_FUNCTION\_ALLOW\_EXTERNAL=chai” in our DockerImage.

chai version is 5.0.3  
npm version is 9.5.1

Can you help me? Is there a way on how to use an external npm library inside a Function node? Am i doing somthing wrong?  
Thanks

---

<div class="post-metadata">

**Author:** ![n8n](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/n8n/32/196425_2.png) [@n8n](https://community.n8n.io/u/n8n)\
**Post date:** [January 29, 2024, 11:55pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/2 "2024-01-29T23:55:10Z")

</div>

It looks like your topic is missing some important information. Could you provide the following if applicable.

- **n8n version:**
- **Database (default: SQLite):**
- **n8n EXECUTIONS\_PROCESS setting (default: own, main):**
- **Running n8n via (Docker, npm, n8n cloud, desktop app):**
- **Operating system:**

---

<div class="post-metadata">

**Author:** ![Sengerio](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/sengerio/32/18071_2.png) [@Sengerio](https://community.n8n.io/u/Sengerio)\
**Post date:** [January 30, 2024, 7:30am UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/3 "2024-01-30T07:30:08Z")

</div>

Hi, sorry i tought i put some data in my previous message, here are all the info:

- **n8n version: 1.25.1**
- **Database: default**
- **n8n EXECUTIONS\_PROCESS setting: default** Also i have seen that its deprecated and instead now is used EXECUTIONS\_MODE. This setting i have let it default since i did not rewrite it.
- **Running n8n via: Docker** I have a Docker Image configured to self host on a custom domain.
- **Operating system:** The instance of n8n runs on a Machine that is Ubuntu 20.04.6, but is Dockerized so is isolated.

Other configuration on the DockerImage regarding the ENV variables:  
ENV NODE\_FUNCTION\_ALLOW\_EXTERNAL=chai  
ENV EXECUTIONS\_DATA\_PRUNE=true  
ENV EXECUTIONS\_DATA\_MAX\_AGE=336  
ENV EXECUTIONS\_DATA\_PRUNE\_MAX\_COUNT=20000  
ENV N8N\_PUSH\_BACKEND=sse

This is the Function Node:

---

<div class="post-metadata">

**Author:** ![EmeraldHerald](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/emeraldherald/32/35355_2.png) [@EmeraldHerald](https://community.n8n.io/u/EmeraldHerald)\
**Post date:** [January 31, 2024, 3:36pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/4 "2024-01-31T15:36:08Z")

</div>

Hi there @Sengerio 👋 Thanks for the additional information - Maybe @Jon or @marcus could help with this one 🙇‍♂️

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [January 31, 2024, 4:28pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/5 "2024-01-31T16:28:37Z")

</div>

Hey @Sengerio,

It looks like the error has pretty much nailed it… You can’t use `require()` and instead need to use `import`

```auto
Error [ERR_REQUIRE_ESM]: require() of ES Module /usr/local/lib/node_modules/chai/chai.js from /usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js not supported.
Instead change the require of chai.js in /usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver-compat.js to a dynamic import() which is available in all CommonJS modules.

```

I am not familiar with Chai but looking at the docs something like `import { assert } from 'chai';` might be what you are after.

---

<div class="post-metadata">

**Author:** ![Sengerio](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/sengerio/32/18071_2.png) [@Sengerio](https://community.n8n.io/u/Sengerio)\
**Post date:** [February 1, 2024, 7:26am UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/6 "2024-02-01T07:26:06Z")

</div>

Hi @Jon. Sorry i should i have told in the message sooner. I’ve already tried that solution, but it gives me error. Here, let me show you.

The error is:

> /usr/local/lib/node\_modules/n8n/node\_modules/n8n-nodes-base/dist/nodes/Code:1  
> module.exports = async function() {import { assert } from ‘chai’;  
> ^^^^^^  
> SyntaxError: ‘import’ and ‘export’ may only appear at the top level  
> at makeNiceSyntaxError (/usr/local/lib/node\_modules/n8n/node\_modules/@n8n/vm2/lib/transformer.js:41:16)  
> at transformer (/usr/local/lib/node\_modules/n8n/node\_modules/@n8n/vm2/lib/transformer.js:84:8)  
> at NodeVM.run (/usr/local/lib/node\_modules/n8n/node\_modules/@n8n/vm2/lib/nodevm.js:485:17)  
> at JavaScriptSandbox.runCodeAllItems (/usr/local/lib/node\_modules/n8n/node\_modules/n8n-nodes-base/dist/nodes/Code/JavaScriptSandbox.js:51:45)  
> at Object.execute (/usr/local/lib/node\_modules/n8n/node\_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:116:40)  
> at Workflow.runNode (/usr/local/lib/node\_modules/n8n/node\_modules/n8n-workflow/dist/Workflow.js:698:42)  
> at /usr/local/lib/node\_modules/n8n/node\_modules/n8n-core/dist/WorkflowExecute.js:656:68

It’s as the function node could not allow the implementation of the import function. Here is the updated node:

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [February 1, 2024, 3:04pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/7 "2024-02-01T15:04:41Z")

</div>

Ahh yeah I forgot about that, There are some packages that may not work at all it could be that Chai is one of them. If I get a chance I will build a docker image with it and see if I can find a way to get it working.

---

<div class="post-metadata">

**Author:** ![CristianG](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/cristiang/32/74203_2.png) [@CristianG](https://community.n8n.io/u/CristianG)\
**Post date:** [February 2, 2024, 7:03pm UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/8 "2024-02-02T19:03:56Z")

</div>

Hey,

I encountered the same problem with another package ([Can't use 'require package' in custom function after updating to 1.20.0 - #3 by CristianG](http://community.n8n.io/t/cant-use-require-package-in-custom-function-after-updating-to-1-20-0/34406/3)).  
I believe it has to do with how these new packages are developed.  
I am not a skilled Node developer, so I cannot identify the exact issue.  
In my case, the solution was to use an older version of the npm package.  
Try using an older version of the chai package, found here: [chai - npm](https://www.npmjs.com/package/chai?activeTab=versions)

@Jon, I guess we will need to figure out how to import these newer packages that are using ESM.  
I think this is a starting point: [javascript - Error [ERR\_REQUIRE\_ESM]: require() of ES Module not supported - Stack Overflow](https://stackoverflow.com/questions/69081410/error-err-require-esm-require-of-es-module-not-supported), but I couldn’t fully understand the leading cause of the error, other than the fact that the developers used ESM in their packages.  
As more developers are expected to start using it soon, we will need to find a way to import these packages into a Code node.

---

<div class="post-metadata">

**Author:** ![tempire](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/tempire/32/35952_2.png) [@tempire](https://community.n8n.io/u/tempire)\
**Post date:** [February 27, 2024, 12:00am UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/9 "2024-02-27T00:00:34Z")

</div>

Goodness. So n8n can’t handle nodes that use esm modules? Yikes.

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [February 27, 2024, 7:59am UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/10 "2024-02-27T07:59:12Z")

</div>

@tempire Would need to do some digging but that could be the case because of the javascript sandbox, While it may not be directly useable in the code node though you could still build a custom node that uses it or use execute command to run a script that is using it.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/195824_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [May 27, 2024, 7:59am UTC](https://community.n8n.io/t/code-node-cannot-use-external-npm-module/36097/11 "2024-05-27T07:59:19Z")

</div>

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