I’m encountering issues while trying to use a third-party npm library in my Nuxt.js application, which utilizes Vue and TypeScript. Specifically, I want to incorporate a package named array-randomizer, which relies on another library called random-generator.
Within my helpers/data.ts file, I have the following code:
import { randomize } from "array-randomizer";
export const myList: string[] = ["x", "y", "z"];
export const shuffledList = randomize(myList, "myseed");
However, when I start the development server, I receive this error message:
array-randomizer.js:520 Uncaught ReferenceError: random-generator is not defined
at Object.<anonymous> (array-randomizer.js:520:25)
at node_modules/array-randomizer/index.js (array-randomizer.js:580:12)
at __require (chunk-ABC123.js:4:35)
I’ve confirmed that both packages are listed in my package.json:
"dependencies": {
"array-randomizer": "^2.1.0",
"nuxt": "^3.17.1",
"random-generator": "^2.5.3",
"vue": "^3.5.13"
},
"devDependencies": {
"@types/array-randomizer": "^2.0.5",
"@types/random-generator": "^2.1.2"
}
Here’s how my Nuxt configuration looks:
export default defineNuxtConfig({
devtools: { enabled: true },
ssr: false,
modules: ['@nuxt/image']
})
I have attempted to create a declaration file, but it hasn’t resolved the problem. What steps can I take to correctly import and utilize external npm packages in Nuxt while using TypeScript?