google-apps-scriptnpm-scriptsclasp

Unable to run Clasp from npm or yarn script


I have a Google Apps Script project and I am trying to run clasp push from an npm or yarn script. I am able to run clasp push from the command line, but the same command fails when used as a script:

"scripts": {
  "push": "clasp push"
}

When run as npm run push or yarn push, the script fails with the output > Did you provide the correct scriptId?. If I run clasp push directly on the command line it successfully pushes my project files to Google Apps Script.

I have verified the script is executing in the same directory that contains .clasprc.json and appsscript.json. Here is the full output:


> @ push /gs
> cd build && ls -a && clasp push

.               ..              .clasp.json     .clasprc.json   appsscript.json build.gs

> Did you provide the correct scriptId?

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @ push: `cd build && ls -a && clasp push`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the @ push script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

For context, I am trying to do something similar to this blog post: https://labs.clio.com/a-modern-gmail-add-on-build-process-3dd7aad111f5


Solution

  • I was able to diagnose the problem. I found that I had a global login (~/.clasprc.json) that had a different access token from the local .clasprc.json. When running clasp push from the command line, it uses the .clasprc.json file in the root of my project. However when it runs through npm it was using the global login.
    I ran clasp logout and verified the .clasprc.json file in the root of my project and my home folder were both deleted. After logging back in again I was able to successfully push using the npm script