.then error with initApi

This is the error I'm getting:

[0] initApi(req).then((api) => {
[0] ^
[0]
[0] TypeError: initApi(...).then is not a function

Here is my code, I'm using express.js approach:

const path = require("path");
const express = require("express");
const app = express();
const port = 3000;
const Prismic = require("@prismicio/client");
const PrismicH = require("@prismicio/helpers");
const UAParser = require("ua-parser-js");
const fetch = require("node-fetch");
const { response } = require("express");
require("dotenv").config();

// Link Resolver
const handleLinkResolver = (doc) => {
  // Default to homepage
  return "/";
};

app.use((req, res, next) => {
  res.locals.ctx = {
    endpoint: process.env.PRISMIC_ENDPOINT,
    linkResolver: handleLinkResolver,
  };

  res.locals.PrismicH = PrismicH;

  next();
});

const initApi = (req) => {
  return Prismic.createClient(process.env.PRISMIC_ENDPOINT, {
    accessToken: process.env.PRISMIC_ACCESS_TOKEN,
    req,
    fetch,
  });
};

app.set("views", path.join(__dirname, "views"));
app.set("view engine", "pug");

app.get("/", (req, res) => {
  res.render("pages/home");
});

app.get("/about", async (req, res) => {
  initApi(req).then((api) => {
    api
      .query(Prismic.predicate.at("document.type", "about"))
      .then((response) => {
        console.log(response);
        // response is the response object. Render your views here.
        // res.render("pages/about");
      });
  });
});

app.get("/collections", (req, res) => {
  res.render("pages/collections");
});

app.listen(3000);

What could be the error and how to fix this?

Hello Harpreet,

How about converting require() method to import() ? Here are starter project in express.js: GitHub - prismicio/nodejs-blog

For example:

import * as Prismic from "@prismicio/client";

Give this a try, and let me know.

Thanks,
Priyanka