# How to execute package.json scripts from buildkite plugin lifecycle hooks

**URL:** <https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934>\
**Category:** Pipelines\
**Created:** [May 30, 2023, 8:19pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934 "2023-05-30T20:19:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zakk](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zakk](https://forum.buildkite.community/u/Zakk)\
**Post date:** [May 30, 2023, 8:19pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/1 "2023-05-30T20:19:32Z")

</div>

Hi,  
I am creating a buildkite plugin to run the E2E test.(This plugin is a git repository containing only shared E2E tests ). I want to use this plugin in my application’s pipeline and run the test. My question is how can I execute the test script residing in package.json of buildkite plugin from lifecycle hook programatically or any better way?  
Any help is appreciated

---

<div class="post-metadata">

**Author:** ![Michelle](https://avatars.discourse-cdn.com/v4/letter/m/6de8d8/32.png) [@Michelle](https://forum.buildkite.community/u/Michelle)\
**Post date:** [May 30, 2023, 8:30pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/2 "2023-05-30T20:30:04Z")

</div>

Hello @Zakk !

We’re taking a look at this for you and will get back to you as soon as possible.

Thank you!  
Michelle

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [May 30, 2023, 10:04pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/3 "2023-05-30T22:04:30Z")

</div>

Hey @Zakk just following up on your question.

To do this make sure that the test script in the plugin’s `package.json` is correctly defined.

package.json

```auto
"scripts": {
  "test:e2e": "your-e2e-test-command"
},

```

Then in the pipeline configuration, specify a step that executes the test script in the plugin’s package.json. You can use the command attribute to run the desired script.  
yaml

```auto
steps:
  - label: ":test_tube: Run E2E tests"
    command: npm run test:e2e

```

Here are links About how to [add hooks](https://buildkite.com/docs/plugins/writing#step-4-add-a-hook) and the [hook locations](https://buildkite.com/docs/agent/v3/hooks#hook-locations-plugin-hooks) for plugins

Hope this helps!  
Cheers!

---

<div class="post-metadata">

**Author:** ![Zakk](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zakk](https://forum.buildkite.community/u/Zakk)\
**Post date:** [May 30, 2023, 10:28pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/4 "2023-05-30T22:28:03Z")

</div>

Hi @stephanie.atte ,  
Thanks for replying. You mean command should be in the pipeline configuration of the application where I am using this plugin. For example:

Application:

```auto
steps:
  - label: ":test_tube: Run E2E tests"
    command: npm run test:e2e
    plugins:
      - a-github-user/shared-test#v1.0.0:
          

```

shared-test-buildkite-plugin:  
package.json

```auto
"scripts": {
  "test:e2e": "your-e2e-test-command"
},

```

If my understanding is correct then command in my application’s pipeline will execute the script in package.json of buildkite plugin?

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [May 30, 2023, 11:01pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/5 "2023-05-30T23:01:44Z")

</div>

Yes @Zakk , let’s try this out and see how it goes.

---

<div class="post-metadata">

**Author:** ![Zakk](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zakk](https://forum.buildkite.community/u/Zakk)\
**Post date:** [May 30, 2023, 11:12pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/6 "2023-05-30T23:12:18Z")

</div>

@stephanieat I tried but doesn’t seems to be working and nothing shows up in log related to this. Is it possible to execute the package json script from pre-exit hook of the plugin?

---

<div class="post-metadata">

**Author:** ![Zakk](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zakk](https://forum.buildkite.community/u/Zakk)\
**Post date:** [May 31, 2023, 6:48am UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/7 "2023-05-31T06:48:18Z")

</div>

@stephanie.atte My plugin.yml file looks like below:  
I have defined one property which will receive the baseUrl from application to run the cypress test (E2E test) in my plugin. I am not sure if I am missing out anything?  
In this example I am trying to execute a simple script to log instead of running my e2e test for the sake of simplicity and better understanding

plugin.yml

```auto
name: Shareable Test Buildkite Plugin
description: Assists in running e2e test in Buildkite
author: https://github.com/zakk
requirements: []
configuration:
  properties:
    applicationUrl:
      type: string
  additionalProperties: false

```

package.json of buildkite plugin:

```auto
"scripts": {
    "print-console": "sh scripts/script.sh"
  },

```

script.sh is just logging some text

```auto
echo "TestConsole======"

```

Application pipeline using this plugin from feature branch:

```auto
 label: ':Sharable Test'
 command: npm run print-console
 env:
      BUILDKITE_PLUGINS_ALWAYS_CLONE_FRESH: "true"
 plugins:
      - ssh://git@github.com/user/shared-test-buildkite-plugin#test-dev:
             applicationUrl: 'base url of the application'

```

Note: I am able to access the property ‘applicationUrl’ in pre-exit hook but not able to execute any script of package.json available in plugin’s package.json

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [May 31, 2023, 4:28pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/8 "2023-05-31T16:28:21Z")

</div>

Hey @Zakk ,

Thanks for the follow up

Since the package.json is in the plugin. You need to have the npm run print-console in the command hook of the plugin as well.

Cheers!

---

<div class="post-metadata">

**Author:** ![Zakk](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zakk](https://forum.buildkite.community/u/Zakk)\
**Post date:** [May 31, 2023, 8:51pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/9 "2023-05-31T20:51:49Z")

</div>

Thanks for your reply and appreciate your patience. I tried npm command but it shows error ' **node command not found’**  
I already have node Image in my applications pipeline where we are using buildkite plugin

Application’s pipeline.yml

```auto
docker-node: &docker-node
  agents:
    queue: docker
  plugins:
    - ssh://git@github.com/docker-buildkite-plugin#vXXX:
        image: node Image.xx.xx.xx
        propagate-environment: true
        mount-ssh-agent: true
        copy-checkout: false
        user: 'root'

```

Command Hook of buildkite plugin

```auto
npm run print-console

```

---

<div class="post-metadata">

**Author:** ![stephanie.atte](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/stephanie.atte/32/1357_2.png) [@stephanie.atte](https://forum.buildkite.community/u/stephanie.atte)\
**Post date:** [May 31, 2023, 10:00pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/10 "2023-05-31T22:00:00Z")

</div>

Hey Zach

Thanks for the information.

The yml file you sent now is a bit different from the previous one. Can I understand what exactly the use case is. Are you using your own plugin or a Buildkite Plugin, which hooks are being used and where, can you also send the package.json aswell?

docker-node: &docker-node is not a valid syntax and also image: node Image.xx.xx.xx

Please can you forward this to [support@buildkite.com](mailto:support@buildkite.com)  
You also send me a build link and any other relevant information in the email. So I can take a closer look

Cheers!

---

<div class="post-metadata">

**Author:** ![Zakk](https://avatars.discourse-cdn.com/v4/letter/z/9fc348/32.png) [@Zakk](https://forum.buildkite.community/u/Zakk)\
**Post date:** [May 31, 2023, 10:25pm UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/11 "2023-05-31T22:25:57Z")

</div>

Sorry for the confusion, actually that is the different step in pipeline using another plugin for node Image. I will try to phrase my question again .  
**My usecase** : To invoke package.json script of buildkite plugin from command hook of buildkite plugin.  
**Attempt:** written `npm run print-console` in command hook  
**Result:** Getting error stating **npm command not found** when the plugin is invoked by pipeline step of my application(different repository)

What could be the potential reason?

Thanks

---

<div class="post-metadata">

**Author:** ![benmc](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/benmc/32/1159_2.png) [@benmc](https://forum.buildkite.community/u/benmc)\
**Post date:** [June 1, 2023, 12:28am UTC](https://forum.buildkite.community/t/how-to-execute-package-json-scripts-from-buildkite-plugin-lifecycle-hooks/2934/12 "2023-06-01T00:28:48Z")

</div>

Hey there @Zakk!

Ben here 👋 Just jumping in for Stephanie due to timezones.

The reason that you’re getting the `npm command not found` error will be because your instance AMI doesn’t have Node [installed on it by default](https://buildkite.com/docs/agent/v3/elastic-ci-aws#before-you-start-whats-on-each-machine). If you want to have Node be on your machines, and thus be able to run `npm` commands then your best bet is to create a custom AMI based off of the official Buildkite release, tools such as Packer can help with this and the Packer file might look something like this:

```plaintext
packer {
  required_plugins {
    amazon = {
      version = ">= 0.0.2"
      source = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "buildkite_agent" {
  region = "<REGION>"
  source_ami = "<SOURCE AMI>"
  instance_type = "<INSTANCE TYPE>"
  ssh_username = "<SSH_USER>"
  ami_name = "<AMI NAME>"
}

build {
  sources = ["source.amazon-ebs.buildkite_agent"]

  provisioner "shell" {
    inline = [
      "sudo yum update -y",
      "sudo apt install nodejs",
      "bazel version"
    ]
  }
}

```

The other option you have is to run this command within a Docker container. Assuming you have a step which you use to _build_ your image, you could also run tests against the same image earlier on in the pipeline, only pushing the image to something such as ECR if the testing step passes.

In order for us to have a more granular view of your specific setup, it would be helpful if you could email us at [support@buildkite.com](mailto:support@buildkite.com). Given sharing the pipeline on a public forum wouldn’t be advised.

Cheers!
