Prismic.getApi() is not a function but is installed

Hi,
I have installed prismic via:
npm install prismic-javascript prismic-dom --save

I am building a website that only runs with webpack and is static.
When I run

var Prismic = require('prismic-javascript')
console.log(Prismic)

I get

The problem is that I cannot use any function. I tried to run any function and it always returns a TypeError.

var Prismic = require('prismic-javascript')
Prismic.getApi()

I appreciate any help,
thank you!

Hi Philip,

Welcome to the community :slight_smile: I’ll be happy to debug this with you.

Are you passing in your Prismic repository endpoint when running Prismic.getApi()?

For example:
Prismic.getApi("http://your_repository_name.prismic.io/api")

Or like is shown in the docs:

Prismic.api("http://your_repository_name.prismic.io/api", function(error, api) {
  var options = {}; // In Node.js, pass the request as 'req' to read the reference from the cookies
  api.query("", options, function(err, response) { // An empty query will return all the documents
    if (err) {
      console.log("Something went wrong: ", err);
    }
    console.log("Documents: ", response.documents);
  });
});

Thank you!
Yes I am. I tried your code and I still get the “Uncaught TypeError: Prismic.api is not a function” error.

This is my code:

var Prismic = require('prismic-javascript');

console.log(Prismic);
Prismic.api("https://mywebsitename.prismic.io/api", function(error, api) {
  var options = {}; // In Node.js, pass the request as 'req' to read the reference from the cookies
  api.query("", options, function(err, response) { // An empty query will return all the documents
    if (err) {
      console.log("Something went wrong: ", err);
    }
    console.log("Documents: ", response.documents);
  });
});

Thanks!

Hello.

Are you using a framework?

How about

import Prismic from 'prismic-javascript';
1 Like

That worked! Thank you so much. Can you explain why this works and

var Prismic = require('prismic-javascript');

does not?