require npm package inside mock-server docker container (javascript template response) not working

I have a mock-server (https://github.com/jamesdbloom/mockserver) docker container instance with some expectation that should return a javascript template. It would make my life a lot easier if I could use npm packages within that template (namely faker.js) to generate a good fake response.

The mockserver is not able to resolve require(‘faker’) and I’m really not sure why. Here’s what I tried so far:

  1. npm install faker (installs faker in /opt/mockserver/)
    I’ve tried require('faker'), require('/opt/mockserver/faker')

  2. npm install -g faker (installs faker in /usr/lib/)
    I’ve tried require('faker'), require('/usr/lib/faker')

  3. cloning faker.js repo and using it as a local module
    I’ve tried require('/opt/mockserver/faker.js'), require('/opt/mockserver/faker.js/lib')

All of the above did not work. Not using an outside module works fine but I’d really like to solve this issue. The mockserver is not a nodejs application and all I could find online is how to turn a docker into a nodejs app.

Any help is appreciated

Here’s some starter code if anyone wants to give it a try

To create the expectation

curl -v -X PUT "http://localhost:1080/mockserver/expectation" -d '{
    "httpRequest": {
        "path": "/some/path"
    },
    "httpResponseTemplate": {
        "template": "var faker = require('/opt/mockserver/faker.js');nreturn {'statusCode': 200, 'body': JSON.stringify({'firstName': faker.name.firstName(), 'lastName': faker.name.lastName()})};",
        "templateType": "JAVASCRIPT"
    }
}'

with this response template

var faker = require('/opt/mockserver/faker.js');

return {
    'statusCode': 200,
    'body': JSON.stringify({
        'firstName': faker.name.firstName(),
        'lastName': faker.name.lastName()
    })
};

To validate:

curl -v -X GET "http://localhost:1080/some/path"

Thanks 🙂

Source: StackOverflow