JS Example has error with loading routes

I've downloaded the JS example from here: https://github.com/sapphiredev/examples/tree/main/examples/with-javascript The bot runs fine apart from a few issues with loading the routes
Error when loading 'C:\Users\Jacob\Downloads\sapph\src\routes\hello-world.js': TypeError: Cannot read properties of undefined (reading 'GET')
at Object.<anonymous> (C:\Users\Jacob\Downloads\sapph\src\routes\hello-world.js:11:11)
Error when loading 'C:\Users\Jacob\Downloads\sapph\src\routes\hello-world.js': TypeError: Cannot read properties of undefined (reading 'GET')
at Object.<anonymous> (C:\Users\Jacob\Downloads\sapph\src\routes\hello-world.js:11:11)
and
at _RouteLoaderStrategy.preload (C:\Users\Jacob\Downloads\sapph\node_modules\@sapphire\pieces\dist\cjs\lib\strategies\LoaderStrategy.cjs:50:17)
Error when loading 'C:\Users\Jacob\Downloads\sapph\src\commands\General\eval.js': Error: Cannot find module 'C:\Users\Jacob\Downloads\sapph\node_modules\@sapphire\type\prebuild\type-node-v127-win32-x64-unknown-unknown\type.node'
at _RouteLoaderStrategy.preload (C:\Users\Jacob\Downloads\sapph\node_modules\@sapphire\pieces\dist\cjs\lib\strategies\LoaderStrategy.cjs:50:17)
Error when loading 'C:\Users\Jacob\Downloads\sapph\src\commands\General\eval.js': Error: Cannot find module 'C:\Users\Jacob\Downloads\sapph\node_modules\@sapphire\type\prebuild\type-node-v127-win32-x64-unknown-unknown\type.node'
Not sure what's going on here. I'm running node v22.11.0 and npm 10.9.0, I've ran npm install and then npm watch. Many thanks!
GitHub
examples/examples/with-javascript at main · sapphiredev/examples
Various examples of setting up your bot with the Sapphire Framework - sapphiredev/examples
10 Replies
Favna
Favna4w ago
The second is because you need to install a build toolchain, see https://github.com/nodejs/node-gyp#on-windows The first... is because the code wasn't updated to Sapphire Plugin v7. If you don't need REST api plugin, just delete those files for now. If you do, then refer to the breaking changes, also we'd very much appreciate a PR ❤️ https://github.com/sapphiredev/plugins/blob/main/packages/api/CHANGELOG.md#sapphireplugin-api700---2024-11-02
GitHub
GitHub - nodejs/node-gyp: Node.js native addon build tool
Node.js native addon build tool. Contribute to nodejs/node-gyp development by creating an account on GitHub.
Jayy
JayyOP4w ago
Thanks for the quick response! I'm guessing the docs need updating too with the new example code for 7.00? Unless the docs are specifically designed for an older version? https://www.sapphirejs.dev/docs/Guide/plugins/API/adding-routes I'll try my best to workout how to fix it, unfortunately I'm very new to Node and Sapphire so might take some time, will try and do a PR 😅
Sapphire Framework
How to add routes | Sapphire
First we create a new folder called routes in the src directory. And make a new file hello-world
Jayy
JayyOP4w ago
Oh, nevermind - that was pleasantly simple!
const { methods, Route } = require('@sapphire/plugin-api');

class UserRoute extends Route {
constructor(context, options) {
super(context, {
...options,
route: ''
});
}

async run(_, Response) {
return response.json({ message: 'Landing page!' });
}
}

module.exports = {
UserRoute
};
const { methods, Route } = require('@sapphire/plugin-api');

class UserRoute extends Route {
constructor(context, options) {
super(context, {
...options,
route: ''
});
}

async run(_, Response) {
return response.json({ message: 'Landing page!' });
}
}

module.exports = {
UserRoute
};
Changed it to this, which worked without issue. Is that the correct syntax to use? If so, I can open up a PR for both the examples and documentation 🙂 @Boomeravna
Favna
Favna4w ago
the docs are also still outdated yes and there are many example files of how it should be here: https://github.com/skyra-project/skyra/tree/main/src/routes (TS though, but it comes down to mostly the same) the new design is very much inspired by Nuxt which in turn draws some inspiration from Next and other file-based routing libraries so it should be very intuitive
Jayy
JayyOP4w ago
Okay right, aye, I copied it from there and just removed the type annotations
Favna
Favna4w ago
yeah and the way it's exported if you're using CJS ofc
Favna
Favna4w ago
for the docs (https://github.com/sapphiredev/website) the input is TS only and it gets converted to JS-ESM and JS-CJS programatically
GitHub
GitHub - sapphiredev/website: Documentation for Sapphire Project
Documentation for Sapphire Project. Contribute to sapphiredev/website development by creating an account on GitHub.
Jayy
JayyOP4w ago
Oh got you, that's clever What's CJS?
Favna
Favna4w ago
CommonJS. For example the module.exports stuff
Jayy
JayyOP4w ago
Ahh okay awesome
Want results from more Discord servers?
Add your server