在Sails项目中集成sails-auth库失败,启动触发TypeError错误
TypeError: sails.getBaseurl is not a function with sails-auth Hey there, that error pops up because of a version compatibility issue between sails-auth and your current Sails.js version. Let's break down why it's happening and how to fix it:
Why this happens
The sails.getBaseurl() method was removed or renamed in newer Sails.js versions (post-v1.x, roughly), but sails-auth hasn't been updated in a while—it still relies on this old method. When you run sails lift, the auth hook tries to call a function that no longer exists in your Sails instance.
Solutions to try
1. Downgrade Sails.js to a compatible version
This is the quickest fix if you don't mind using an older Sails release that works with sails-auth:
- Uninstall your current Sails version:
npm uninstall sails - Install a compatible Sails v1.x release (the last version range sails-auth supports):
npm install sails@1.x --save - Restart your app:
sails lift
2. Patch the sails-auth code manually
If you want to keep your current Sails version, you can modify the sails-auth hook to use the modern replacement for getBaseurl():
- Navigate to the auth hook file in your node_modules:
/home/egwuenu/Desktop/shopper/server/node_modules/sails-auth/dist/api/hooks/auth/index.js - Search for every instance of
sails.getBaseurl()and replace it withsails.config.baseUrl - Important: Make sure you've set
baseUrlin your Sails config! Add this toconfig/custom.js(or the appropriate environment config file):module.exports.custom = { baseUrl: 'http://localhost:1337' // Replace with your app's actual base URL }; - Save the changes and run
sails liftagain
⚠️ Note: Patching node_modules directly is temporary—any npm install or npm update will overwrite your changes. For a permanent fix, fork the sails-auth repo, apply the patch there, and install your forked version via npm.
3. Switch to a maintained auth alternative
Since sails-auth is no longer actively maintained, you might want to use a more up-to-date solution long-term:
- Integrate Passport.js directly into your Sails app (Sails has official guidance on this)
- Use a maintained wrapper like
passport-sails
内容的提问来源于stack exchange,提问作者lauragift21

