Cannot import IPFS

I’m trying to get anything to run on the Windows OS after importing IPFS, but I’m stuck on the import statement.

ReferenceError: TextDecoder is not defined                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    

> 1 | const IPFS = require('ipfs');     

I’m using Node version v14.16.1. Are there extra requirements to get a working ‘Hello world’ example running in Windows?

1 Like

Hello jinhale! Could you tell me what steps you performed so I may attempt to replicate them? (How did you install Node, how are you running the js?)

I installed a new project, npx create-react-app ipfs-foo --template typescript, then I installed IPFS in the newly created project, yarn add ipfs, then I created a new file src\utils.ts:

const IPFS = require('ipfs');

interface ipfsMemoInterface {
    [index:string]: any,

const ipfsCreateMemo = {};

export async function createIpfs(props = {}) {
  const memo = String(Object.entries(props)) || 'createIpfs';

  if (memo in ipfsCreateMemo === false) {
      console.log(Object.keys(IPFS)); // This verifies that IPFS is imported correctly
      // The error always happens next
      ipfsCreateMemo[memo] = await IPFS.create();
  // never gets to this point
  return ipfsCreateMemo[memo];

export async function getLatest() {
  const ipfs = await createIpfs();

  return ipfs.refs.local();

and I created src\utils.test.ts:

import createIpfs from './utils';

test('Test IPFS create method', async () => {
    // expect.assertions(1);
    const mockCreateIpfs = jest.fn(async () => await createIpfs());

    await mockCreateIpfs();


test("Read the latest", async () => {
    const mockGetLatest = jest.fn(async () => await getLatest());

    await mockGetLatest();


finally I ran npm test which runs a script, react-scripts test, and the result is that the tests fail to even run (0 tests are run out of 2)!

I can replicate this error now using npm version 7.7.6 and node version v12.21.0 on Linux and a very simple setup:

Run npm init, then yarn add ipfs ipfs-core and yarn add --dev jest.

// index.js
const IPFS = require('ipfs-core');

const ipfsMemo = {};

async function getIPFS(options) {
        const memo = JSON.stringify(options);

        if (memo in ipfsMemo) {
                return ipfsMemo;

        const ipfs = await IPFS.create(options);

        ipfsMemo[memo] = ipfs;

        return ipfsMemo[memo];

async function getLatest() {
        const node = await getIPFS();

        return await node.refs.local();

module.exports = {
        getIPFS, getLatest,

// index.test.js
const { getLatest, getIPFS, } = require('./index.js');

describe("Test IPFS is configured correctly", () => {
        test('itialialize IPFS nodes', async () => {
                const node1 = await getIPFS();
                const node2 = await getIPFS();


Run npm test.

I’m surprised that the error happens whether I use ipfs or 'ipfs-core`.

Does this just mean that IPFS is not compatible with NodeJS? Is support just limited to a narrow set of browsers?

I’m testing using jest in case that wasn’t clear. Is Jest fundamentally incompatible somehow? If that’s the case how could I do unit tests? Do I have to roll my own testing framework?

I get the same error if I try to add a unit test of IPFS like before in the 101 example.

I don’t have any experience with Jest, however a minimal NodeJS example:

npm install ipfs-core

const IPFS = require('ipfs-core')
const fs = require('fs');
 async function toIPFS() {
	 const node = await IPFS.create()
	 const data = fs.readFileSync('./Icon.png')
	 const results = await node.add(data)

Run with: node index.js

Icon.png is just my avatar, it should output the CID (QmcuGJ7ukmuTnVkYrPSc32irpwKpM78p3DSwvQn9J12SX3) and work without a hitch. Not sure why you’re getting TextDecoder is not defined.

FWIW: NodeJS v16.0.0, npm 7.11.1.

1 Like

Using Node similar to yours and not Jest, does indeed work. I simply needed to replace require expressions with import statements.

Node v 16.0.0 npm v 7.10.0

1 Like