Playing around with the Admin API in the Node shell/REPL?

Hi,
I’m just trying to familiarize myself with the Ghost APIs and tried to require the Admin API with
const GhostAdminAPI = require('@tryghost/admin-api')
in the node shell with running the node command in the Ghost directory.
It throws this error though:
$ node
> const GhostAdminAPI = require(’@tryghost/admin-api’)
Thrown:
{ Error: Cannot find module ‘@tryghost/admin-api’
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18) code: ‘MODULE_NOT_FOUND’ }

The @tryghost/admin-api is in the respective node_modules folder.

Can anyone help?

If you’re looking for some help, it’s important to provide as much context as possible so that people are able to assist you. Try to always mention:

  • What’s your URL? This is the easiest way for others to debug your issue
  • What version of Ghost are you using? 3.2.0
  • What configuration?
  • What browser?
  • What errors or information do you see in the console?
  • What steps could someone else take to reproduce the issue you’re having?

Hey @tillrahn :wave: To be able to access ‘@tryghost/admin-api’ module from Node REPL you need to run it from the folder that is having node_models with this package installed. You can make sure the module is there with:

➜ tree node_modules/@tryghost/admin-api  
node_modules/@tryghost/admin-api
├── lib
│   ├── index.js
│   └── token.js
├── LICENSE
├── package.json
└── README.md

1 directory, 5 files

I just tried this in one of the projects that uses admin-api and it worked fine requiring it from the REPL :wink:

1 Like

Thank you, @gargol. Made it work. Made a stupid mistake assuming the module was installed in the main Ghost repo, which it wasn’t… :smiley: