Having hard time using crypto-js module on code node

Describe the problem/error/question

I have to create a specific code to use some endpoint. The code includes the encryption through SHA1 of a string. I am having trouble using crypto-js to do create the hashed string

What is the error message (if any)?

The error is:

Cannot find module ‘crypto-js’ [line 15, for item 0]
VMError

But I just installed the crypto-js module, as you can see here:

and I have the NODE_FUNCTION_ALLOW_BUILTIN set to *, as you can see here:

I am using N8N selfhosted and it’s all up to date.

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:
  • n8n version: 1.78.1
  • Database (default: SQLite): SQLite
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Docker
  • Operating system: Linux

NOTE: I didn’t test this, so you may run into issues

I don’t think crypto-js is built in, so you would need to install it, I think if you made a Dockerfile

FROM docker.n8n.io/n8nio/n8n
RUN npm install crypto-js

and then changed your n8n to build

n8n:
build: .
#image: docker.n8n.io/n8nio/n8n
…rest of your compose

then to build run docker compose build
docker compose up -d --build

And then to update to newer versions run
docker pull docker.n8n.io/n8nio/n8n
docker compose up -d --build

Hi there,

The module crypto-js is not built-in n8n, only the crypto module, that is part of nodejs.

I would suggest you just use the crypto module, but if you really need to use the crypto-js, then you have to allow it as an external function:

NODE_FUNCTION_ALLOW_EXTERNAL=crypto-js

The crypto module, apparently, isn’t working.
Here’s the error I got trying to hashing a SHA1 string.
All of this is for generate a token for an API request, and the documentation is very bad made.


Error: error:1E08010C:DECODER routines::unsupported at Sign.sign (node:internal/crypto/sig:128:29) at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Crypto/Crypto.node.js:499:37) at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:627:42) at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:878:62 at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:1211:20

I am able to execute the request from Postman, using a pre-request script. Here is the script, to reference:

const crypto = require("crypto-js");
const username = "***";
const password = "***";
const key = "***";
const secret = "***";

let idst = "12345";

pm.environment.set("userIdst", idst);

const params = [idst];
const sha1Input = params.join(",") + "," + secret;

const sha1Hashed = crypto.SHA1(sha1Input).toString().toLowerCase();

const authorizationCode = Buffer.from(`${key}:${sha1Hashed}`).toString("base64");

pm.environment.set("authorization_code", authorizationCode);
pm.environment.set("username", username);
pm.environment.set("password", password);

I need to use the authorizationCode in header for the subsequent request.

Show the code you’re using in the n8n code node that generated this error.

Here’s the code:

const crypto = require('crypto-js');

const prms = {
  username:"***",
  password: "***",
  key: "***",
  secret: "***",
  idst: $json.id_user
}

let idst = $json.id_user;
let params = [idst];
let sha1Input = params.join(",") + "," + prms.secret;

let sha1Hashed = crypto.SHA1(sha1Input).toString().toLowerCase();

return {prms: prms, sha1: sha1Hashed}

And here’s the actual error:

Cannot find module ‘crypto-js’ [line 15, for item 0]

{
  "errorMessage": "Cannot find module 'crypto-js' [line 15, for item 0]",
  "errorDescription": "VMError",
  "errorDetails": {},
  "n8nDetails": {
    "nodeName": "Dati per chiamata formaLMS",
    "nodeType": "n8n-nodes-base.code",
    "nodeVersion": 2,
    "itemIndex": 0,
    "n8nVersion": "1.79.3 (Self Hosted)",
    "binaryDataMode": "default",
    "stackTrace": [
      "VMError: Cannot find module 'crypto-js'",
      "    at Resolver.resolveFull (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:126:9)",
      "    at Resolver.resolve (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/resolver.js:121:15)",
      "    at resolve (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/nodevm.js:317:21)",
      "    at VM2 Wrapper.apply (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/bridge.js:490:11)",
      "    at requireImpl (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/setup-node-sandbox.js:90:19)",
      "    at require (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/setup-node-sandbox.js:171:10)",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:15:16",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code:20:2",
      "    at VM2 Wrapper.apply (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/bridge.js:490:11)",
      "    at NodeVM.run (/usr/local/lib/node_modules/n8n/node_modules/@n8n/vm2/lib/nodevm.js:497:23)",
      "    at JavaScriptSandbox.runCodeEachItem (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/JavaScriptSandbox.js:76:45)",
      "    at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Code/Code.node.js:152:40)",
      "    at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:627:42)",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:878:62",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:1211:20"
    ]
  }
}

You’re mistaking the ‘crypto-js’ module for the ‘crypto’ module that is built-in n8n.

Crypto-js is not built-in. If you really want to use it, you’ll have to create a new docker image, and allow it in the environment variables.

It’s easier to use the built-in crypto module.

The crypto module is not working. Here’s the error:

{
  "errorMessage": "error:1E08010C:DECODER routines::unsupported",
  "errorDetails": {},
  "n8nDetails": {
    "n8nVersion": "1.79.3 (Self Hosted)",
    "binaryDataMode": "default",
    "stackTrace": [
      "Error: error:1E08010C:DECODER routines::unsupported",
      "    at Sign.sign (node:internal/crypto/sig:128:29)",
      "    at ExecuteContext.execute (/usr/local/lib/node_modules/n8n/node_modules/n8n-nodes-base/dist/nodes/Crypto/Crypto.node.js:499:37)",
      "    at WorkflowExecute.runNode (/usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:627:42)",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:878:62",
      "    at /usr/local/lib/node_modules/n8n/node_modules/n8n-core/dist/execution-engine/workflow-execute.js:1211:20"
    ]
  }
}

I would prefer to use the crypto module, but can’t manage to make it work. That’s why I though about the crypto-js, because in Postman that is working.

There is probably an error with the key.
Refer to this Question:

So, after a few try, I ended up using a script saved locally and a command node to execute it.

I wasn’t able to make the crypto node working, no matter what I tried.

Here’s the script:

const crypto = require('crypto');

const input = process.argv[2]; 
const key = process.argv[3]; 
const secret = process.argv[4];


if (!input || !key || !secret) {
  console.log('Usage: node sha1_base64_json.js <input_string> <key> <secret>');
  process.exit(1);
}


const sha1Hashed = crypto.createHash('sha1').update(`${input},${secret}`).digest('hex').toLowerCase();


const authorizationCode = Buffer.from(`${key}:${sha1Hashed}`).toString("base64");


const result = {
  sha1_hash: sha1Hashed,
  authorization_code: authorizationCode
};

console.log(JSON.stringify(result, null, 2));

Using a command node is working, but, if you can catch what I did wrong using the crypto node, that would be helpful.

Here’s the setting I used and I think they should be right. The “input_string” in the script, is the “Value” in the setting.

You just need to use a valid private key so that the node can decode. That’s why you get the error.

As Jon put it in his answer (I'm having trouble signing a SHA1 string encoded in Base64 - #4 by Jon), typically the Private Key would be something like:

-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAK…
abc…
-----END RSA PRIVATE KEY-----

The Private Key must be in the correct format so the Crypto node can use it.

That didn’t work either. I added a RSA private key, but throw an error and didn’t encrypt.

In the end, I built my own n8n with crypto-js module and use the command node to execute a script.

FROM n8nio/n8n:latest 

USER root

# Installa crypto-js come dipendenza globale
RUN npm i crypto-js

# Ritorna all'utente non root per sicurezza
USER node
1 Like

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