Vue.js中调用crunker库fetchAudio函数报错:TypeError提示该函数不是函数
Hey there! Let's get to the bottom of that TypeError you're seeing with Crunker in your Vue project.
The core issue here is almost certainly a mismatch between how you're importing/using Crunker and how the library is actually exported (which can vary by version). Since you confirmed the fetchAudio function exists in the module, let's fix the import/usage step:
1. Check your Crunker version and adjust usage accordingly
Crunker changed its API structure between major versions, so let's cover both cases:
If you're using Crunker v1.x or newer:
This version exports methods as static properties on a default object. Your initial import should work, but sometimes Webpack's module resolution can be finicky. Try using a namespace import instead to ensure you're grabbing the full export:import Aplayer from 'vue-aplayer'; import * as Crunker from 'crunker'; // Now call the method Crunker.fetchAudio('a.mp3', 'as.mp3');If you want to debug further, add a
console.log(Crunker)right after importing—this will show you exactly what's being imported, so you can confirmfetchAudiois present.If you're using Crunker v0.x (older versions):
This version exports a class, sofetchAudiois an instance method, not a static one. You'll need to create a Crunker instance first:import Aplayer from 'vue-aplayer'; import Crunker from 'crunker'; // Create an instance first const crunker = new Crunker(); crunker.fetchAudio('a.mp3', 'as.mp3');
2. Verify the module's export structure
If you're still stuck, take a quick look at the actual Crunker module in your node_modules folder. Open node_modules/crunker/dist/crunker.js and check the export:
- If you see something like
export default { fetchAudio, ... }, use the static method approach (v1.x+). - If you see
export default class Crunker { ... }, you need to instantiate it first (v0.x).
One last quick check: make sure you actually installed Crunker properly by running npm install crunker (or yarn add crunker) in your project directory—sometimes missing dependencies can cause weird import issues too!
内容的提问来源于stack exchange,提问作者shirish

