How to connect to Pinecone using Code node?

Hi all!

I need to connect to the Pinecone vector store to perform different operations - for example, delete some vectors or completely delete all vectors from some index.

I can do this using simple python script:

import os
from pinecone import Pinecone
from dotenv import load_dotenv
load_dotenv()

# env var
api_key = os.getenv("PINECONE_API_KEY")

# init
pc = Pinecone(api_key)
index = pc.Index("my_index_name")

# clear the index if it contains vectors
if index.describe_index_stats().total_vector_count != 0:
    index.delete(delete_all=True)
else:
    print("The current index in the Pinecone vector store has no records.")

How can I do the same using the Code node in n8n?

I tried something like this to setup connection:

const Pinecone = require("pinecone-sdk");
const pineconeClient = new Pinecone.Client("MY_API_KEY", "MY_INDEX_NAME");
pineconeClient.connect();

However I get the error: “Problem in node ‘Code’. Cannot find module ‘pinecone-sdk’ [line 1]”.

I also tried setting environment variables to allow importing built-in and external modules:

export NODE_FUNCTION_ALLOW_BUILTIN=*
export NODE_FUNCTION_ALLOW_EXTERNAL=*

However I get another error: “Problem in node ‘Code’. ‘import’ and ‘export’ may only appear at the top level [line 1]”.

So, can someone help me implement the same logic as in my Python script but in the Code node?

Information on n8n setup

  • n8n version: 1.42.1 (Cloud)
  • Database (default: SQLite): NA
  • n8n EXECUTIONS_PROCESS setting (default: own, main): NA
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Cloud
  • Operating system: NA
1 Like

In order to use custom javascript packages you must be running n8n locally on your own server. Preferably via docker.

See here how to enable these external packages:

By setting this environment variable before starting.

export NODE_FUNCTION_ALLOW_EXTERNAL=pinecone

The above isnt javascript code, its “bash”, an environment variable, its entered via the command line/terminal when you setup via docker.

1 Like

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