0

Is it possible to include code from another module into your dangerfile.js or dangerfile.ts? Specifically I mean your code, not some dependency defined in your package.json.

I've tried it with a few different styles of include/require in both JS and TS and couldn't get it to work.

Ex of how I've tried to import it (silly naming to make it more clear and also I've tried using import rather than require):

const myDangerLib = require('./danger/lib');

I keep getting errors like this:

Unable to evaluate the Dangerfile
7
 ReferenceError: myDangerLib is not defined
8
    at Object.<anonymous> (dangerfile.ts:6:11)

Weird thing is that it looks from the docker run command output from the GitHub Action that I'm using like the whole repo is mounted into the container, so I don't know why my lib file would be not found. Here's my GitHub Action step:

      - name: Danger
        uses: danger/danger-js@9.1.8
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Any ideas why this is not working? Or is it just not possible to include code from another one of your modules with DangerJS?

Thanks

maxjeffos
  • 63
  • 3

1 Answers1

0

You can import local file but you'd better not to import "danger".

Hey there, it looks like you're trying to import the danger module. Turns out that the code you write in a Dangerfile.js is actually a bit of a sneaky hack.

When running Danger, the import or require for Danger is removed before the code is evaluated. Instead all of the imports are added to the global runtime, so if you are importing Danger to use one of it's functions - you should instead just use the global object for the root DSL elements.

There is a spectrum thread for discussion here:

You can use exposed api from danger directly such as message, fail or warn

user3003238
  • 1,517
  • 10
  • 17