algolia / gatsby-plugin-algolia

A plugin to push to Algolia based on graphQl queries
https://yarn.pm/gatsby-plugin-algolia
Apache License 2.0
179 stars 45 forks source link

npm ERR! code ELIFECYCLE, npm ERR! errno 1 ... #66

Open amcc opened 4 years ago

amcc commented 4 years ago

I've had algolia running fine with a different API, now I'm trying to get it to work with Sanity.io and have what i think is a very simple query to get going with. This query works fine in graphiQL:

Here's the code, and to debug i put all the API credentials in the code and took it out of .env.production incase there was an issue there. I'm building locally:

// gatsby-config.js
const myQuery = `{
  allSanityPeople {
    edges {
      node {
        displayName
      }
    }
  }
}`

const queries = [
  {
    query: myQuery,
    transformer: ({ data }) =>
      data.allSanityPeople.edges.map(({ node }) => node), // optional
    indexName: "MyIndexName", // overrides main index name, optional
    settings: {
      // optional, any index settings
    },
    matchFields: ["slug", "modified"], // Array<String> overrides main match fields, optional
  },
]

In my config settings:

{
      resolve: `gatsby-plugin-algolia`,
      options: {
        appId: "myappid",
        // Careful, no not prefix this with GATSBY_, since that way users can change
        // the data in the index.
        apiKey: "myAdminAPIKey",
        // indexName: process.env.ALGOLIA_INDEX_NAME, // for all queries
        queries,
        chunkSize: 10000, // default: 1000
        settings: {
          // optional, any index settings
        },
        enablePartialUpdates: true, // default: false
        matchFields: ["slug", "modified"], // Array<String> default: ['modified']
      },
    },
Algolia: 1 queries to index
Algolia: query 0: executing query
⠋ onPostBuild
not finished onPostBuild - 0.159s
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gatsby-starter-default@0.1.0 build: `gatsby build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the gatsby-starter-default@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alistairmcclymont/.npm/_logs/2020-05-05T19_08_29_715Z-debug.log
The terminal process terminated with exit code: 1

From the log mentioned above:

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/node',
1 verbose cli   '/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/npm',
1 verbose cli   'run',
1 verbose cli   'build'
1 verbose cli ]
2 info using npm@6.14.4
3 info using node@v13.13.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle gatsby-starter-default@0.1.0~prebuild: gatsby-starter-default@0.1.0
6 info lifecycle gatsby-starter-default@0.1.0~build: gatsby-starter-default@0.1.0
7 verbose lifecycle gatsby-starter-default@0.1.0~build: unsafe-perm in lifecycle true
8 verbose lifecycle gatsby-starter-default@0.1.0~build: PATH: /Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/alistairmcclymont/Sites/RCA/rca2020public/node_modules/.bin:/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin:/Applications/MAMP/bin/php/php7.1.8/bin:/Applications/MAMP/Library/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/usr/local/bin
9 verbose lifecycle gatsby-starter-default@0.1.0~build: CWD: /Users/alistairmcclymont/Sites/RCA/rca2020public
10 silly lifecycle gatsby-starter-default@0.1.0~build: Args: [ '-c', 'gatsby build' ]
11 silly lifecycle gatsby-starter-default@0.1.0~build: Returned: code: 1  signal: null
12 info lifecycle gatsby-starter-default@0.1.0~build: Failed to exec build script
13 verbose stack Error: gatsby-starter-default@0.1.0 build: `gatsby build`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:315:20)
13 verbose stack     at ChildProcess.<anonymous> (/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:315:20)
13 verbose stack     at maybeClose (internal/child_process.js:1026:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid gatsby-starter-default@0.1.0
15 verbose cwd /Users/alistairmcclymont/Sites/RCA/rca2020public
16 verbose Darwin 19.4.0
17 verbose argv "/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/node" "/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/npm" "run" "build"
18 verbose node v13.13.0
19 verbose npm  v6.14.4
20 error code ELIFECYCLE
21 error errno 1
22 error gatsby-starter-default@0.1.0 build: `gatsby build`
22 error Exit status 1
23 error Failed at the gatsby-starter-default@0.1.0 build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]

I want to try and debug this, but can't understand how - if anyone can help me there too that would be great.

Haroenv commented 4 years ago

These logs don't seem to point to a specific problem or line with the error. Could you make this reproducible in a GitHub project please?

amcc commented 4 years ago

@Haroenv yep that's my quandary too! I'm making a repo now to try to replicate and share. I was wondering whether there were any fundamental problems with my code. I'll post the repo asap

amcc commented 4 years ago

I've now got this working. The suggestion to make a repo was a very good one! It worked out of the box. So i've tried to step back though the settings I had originally to find the problem.

By adding an id into the graphQL it worked, this was the only issue.

In the example up top I am only getting a single field, displayName - effectively the title. It seems Algolia needs an id.

This makes some sense, but perhaps a more useful error can be produced if this happens? Also if there's some advanced way of debugging this that would be helpful.

I've created a repo here that works perfectly with Sanity and Algolia, you can reproduce the error using this and following the instructions below: https://github.com/amcc/gatsby-sanity-algolia

By removing id from web/gatsby-config.js (line 16) you'll see the error. You'll have to add credentials for a Sanity blog (you need the Sanity project id), plus the Algolia credentials of course.

Haroenv commented 4 years ago

Interesting, I expect there to be a clear error in that case "id or objectID required", do you see that now or was it swallowed by something?

amcc commented 4 years ago

I think its getting swallowed, or not there, here's the output from the basic sanity site i made:

...
success Building static HTML for pages - 1.668 s — 9/9 43.51 pages/second
Algolia: 1 queries to index
Algolia: query 0: executing query

lerna ERR! npm run build stderr:
error failed to index to Algolia. Query results do not have 'objectID' or 'id' key
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sanity-gatsby-blog-web@1.0.0 build: `gatsby build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sanity-gatsby-blog-web@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alistairmcclymont/.npm/_logs/2020-05-06T09_47_49_965Z-debug.log

lerna ERR! npm run build exited 1 in 'sanity-gatsby-blog-web'
lerna WARN complete Waiting for 1 child process to exit. CTRL-C to exit immediately.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! sanity-gatsby-blog@1.0.0 build: `lerna run build --parallel`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the sanity-gatsby-blog@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/alistairmcclymont/.npm/_logs/2020-05-06T09_47_50_057Z-debug.log
The terminal process terminated with exit code: 1

here's the second log referenced in the terminal output above, the two logs are identical (except one references the web directory - sanity uses a monorepo with a sanity directory and a web directory for Gatsby):

0 info it worked if it ends with ok
1 verbose cli [
1 verbose cli   '/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/node',
1 verbose cli   '/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/npm',
1 verbose cli   'run',
1 verbose cli   'build'
1 verbose cli ]
2 info using npm@6.14.4
3 info using node@v13.13.0
4 verbose run-script [ 'prebuild', 'build', 'postbuild' ]
5 info lifecycle sanity-gatsby-blog-web@1.0.0~prebuild: sanity-gatsby-blog-web@1.0.0
6 info lifecycle sanity-gatsby-blog-web@1.0.0~build: sanity-gatsby-blog-web@1.0.0
7 verbose lifecycle sanity-gatsby-blog-web@1.0.0~build: unsafe-perm in lifecycle true
8 verbose lifecycle sanity-gatsby-blog-web@1.0.0~build: PATH: /Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/alistairmcclymont/Sites/RCA/test/gatsby-sanity-algolia/web/node_modules/.bin:/Users/alistairmcclymont/Sites/RCA/test/gatsby-sanity-algolia/web/node_modules/.bin:/Users/alistairmcclymont/Sites/RCA/test/gatsby-sanity-algolia/node_modules/.bin:/Users/alistairmcclymont/Sites/RCA/test/node_modules/.bin:/Users/alistairmcclymont/Sites/RCA/node_modules/.bin:/Users/alistairmcclymont/Sites/node_modules/.bin:/Users/alistairmcclymont/node_modules/.bin:/Users/node_modules/.bin:/node_modules/.bin:/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin:/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:/Users/alistairmcclymont/Sites/RCA/test/gatsby-sanity-algolia/node_modules/.bin:/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin:/Applications/MAMP/bin/php/php7.1.8/bin:/Applications/MAMP/Library/bin/:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/usr/local/bin
9 verbose lifecycle sanity-gatsby-blog-web@1.0.0~build: CWD: /Users/alistairmcclymont/Sites/RCA/test/gatsby-sanity-algolia/web
10 silly lifecycle sanity-gatsby-blog-web@1.0.0~build: Args: [ '-c', 'gatsby build' ]
11 silly lifecycle sanity-gatsby-blog-web@1.0.0~build: Returned: code: 1  signal: null
12 info lifecycle sanity-gatsby-blog-web@1.0.0~build: Failed to exec build script
13 verbose stack Error: sanity-gatsby-blog-web@1.0.0 build: `gatsby build`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/index.js:332:16)
13 verbose stack     at EventEmitter.emit (events.js:315:20)
13 verbose stack     at ChildProcess.<anonymous> (/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/lib/node_modules/npm/node_modules/npm-lifecycle/lib/spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:315:20)
13 verbose stack     at maybeClose (internal/child_process.js:1026:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)
14 verbose pkgid sanity-gatsby-blog-web@1.0.0
15 verbose cwd /Users/alistairmcclymont/Sites/RCA/test/gatsby-sanity-algolia/web
16 verbose Darwin 19.4.0
17 verbose argv "/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/node" "/Users/alistairmcclymont/.nvm/versions/node/v13.13.0/bin/npm" "run" "build"
18 verbose node v13.13.0
19 verbose npm  v6.14.4
20 error code ELIFECYCLE
21 error errno 1
22 error sanity-gatsby-blog-web@1.0.0 build: `gatsby build`
22 error Exit status 1
23 error Failed at the sanity-gatsby-blog-web@1.0.0 build script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 1, true ]
amcc commented 4 years ago

Here's a working version from a basic repo: https://github.com/amcc/gatsby-sanity-algolia

The issue was leaving out the id from the graphql

Haroenv commented 4 years ago

I'm going to reopen this so I can investigate why the error wasn't shown later. Glad you found your solution though!

bzmillerboy commented 4 years ago

I had a very similar issue that took me a few days to finally resolve. Mine was a tad more complex in that I was breaking my GraphQL results down into small chunks. Algolia was deduplicating those chunks because I was passing the same ID (many Algolia records for my one record). I had to append the index to my ID in order to give Algolia a unique ID.

const chunkBlocks = input => {
    return input.map(({ node: { slug, _rawBody, id, ...rest } }) => {

        return _rawBody.map((body, index) => {
          const bodyContent = extractContent(body)
          const objectID = id + index
          return {type: "page", objectID, ...slug, bodyContent, ...rest}
        })

    }).flat()
}

I agree that it could be called out more clearly that each record requires a unique ID that we are responsible for sending. Hopefully this post saves someone else the trouble.

amcc commented 4 years ago

I came across the same issue again yesterday, on a different project, found my own issue and remembered the fix!

Haroenv commented 4 years ago

I agree a better error should be shown here. Either the error from Algolia could be shown again, or we can do an isUnique on the objectIds before sending. Open for contributions here!