---
title: "NPM · Nuxt Scripts"
description: "Load IIFE scripts from NPM in your Nuxt app."
canonical_url: "https://scripts.nuxt.com/scripts/npm"
meta:
  "og:description": "Load IIFE scripts from NPM in your Nuxt app."
  "og:image": "https://scripts.nuxt.com/_og/s/o_de3f4eefc72ca1f6.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "NPM · Nuxt Scripts"
  "twitter:image": "https://scripts.nuxt.com/_og/s/o_de3f4eefc72ca1f6.png"
---

> Index of every page on this site: [llms.txt](https://scripts.nuxt.com/llms.txt)

**Scripts**

# **NPM**

[Copy for LLMs](https://scripts.nuxt.com/scripts/npm.md)

**NPM**

[**View source **](https://github.com/nuxt/scripts/blob/main/packages/script/src/runtime/registry/npm.ts)

## Nuxt Config Setup

Add this to your `**nuxt.config.ts**` to load NPM globally. Alternatively you can use the **useScriptNpm** composable for more control.

```
export default defineNuxtConfig({
  scripts: {
    registry: {
      npm: {
        packageName: 'lodash',
        trigger: 'onNuxtReady',
      }
    }
  }
})
```

This config automatically enables **first-party mode ** (bundle). See below to customise.

## useScriptNpm()

The `**useScriptNpm**` composable lets you have fine-grain control over when and how NPM is loaded on your site.

```
const { proxy } = useScriptNpm()

// loads package from CDN
```

Please follow the [**Registry Scripts **](https://scripts.nuxt.com/docs/guides/registry-scripts) guide to learn more about advanced usage.

## First-Party Mode: Privacy Focused Proxy

No extra config needed. The script is bundled and served from your domain instead of a third-party CDN, eliminating an extra DNS lookup and improving load times. [**Learn more. **](https://scripts.nuxt.com/docs/guides/first-party)

**Mode**

Bundle

```
export default defineNuxtConfig({
  scripts: {
    // ✅ First-party mode: bundled
    registry: {
      npm: {
        packageName: 'lodash',
        trigger: 'onNuxtReady',
      },
    },
  },
})
```

## Example

Using NPM in a component.

ConversionButton.vue

```
<script setup lang="ts">
const { proxy } = useScriptNpm()

// noop in development, ssr
// just works in production, client
function handleAction() {
  // loads package from CDN
}
</script>

<template>
  <div>
    <button @click="handleAction">
      Send Event
    </button>
  </div>
</template>
```

## Background

You'd usually install an [**npm**](https://www.npmjs.com/) package and bundle it with your app. Loading it only when needed takes more work: you need a [**dynamic **`**import()**`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import), a separate chunk, and any required transpilation during the build.

The [`**useScriptNpm()**`](https://scripts.nuxt.com/scripts/npm) registry script abstracts this process, allowing you to load immediately invoked function expression (IIFE) builds with a single line of code.

Keep frequently used or critical packages in `**package.json**`. CDN loading is most useful for an occasional, non-critical IIFE.

The three examples below load the same file through the registry, `**useScript**`, and `**useHead**`.

```ts
useScriptNpm({
  packageName: 'js-confetti',
  file: 'dist/js-confetti.browser.js',
  version: '0.12.0',
  provider: 'jsdelivr',
})
```

## [`**useScriptNpm()**`](https://scripts.nuxt.com/scripts/npm)

The [`**useScriptNpm()**`](https://scripts.nuxt.com/scripts/npm) composable uses unpkg by default. Set `**provider**` to `**'jsdelivr'**` or `**'cdnjs'**` for the other supported URL formats.

```ts
function useScriptNpm<T extends Record<string | symbol, any>>(_options: NpmInput) {}
```

For triggers, proxying, and other script options, see [**Registry Scripts**](https://scripts.nuxt.com/docs/guides/registry-scripts).

### Map the loaded global

The generic type describes the proxy, but it does not discover a browser global. Map the loaded library explicitly with `**scriptOptions.use**`:

```ts
interface SomeApi {
  doSomething: () => void
}

const { proxy } = useScriptNpm<SomeApi>({
  packageName: 'some-api',
  scriptOptions: {
    use: () => (window as Window & { SomeApi: SomeApi }).SomeApi,
  },
})

proxy.doSomething()
```

Without `**scriptOptions.use**`, the IIFE still loads, but the composable does not expose its global API through `**proxy**` or `**onLoaded**`.

`**packageName**``string`** required **

The npm package name to load.

`**file**``string`

The specific file path within the package to load.

`**version**``string` = 'latest'

The package version to load.

`**provider**``'jsdelivr' | 'cdnjs' | 'unpkg'` = 'unpkg'

The CDN provider to use for loading the package.

## Example

See the [**Tutorial: Load js-confetti**](https://scripts.nuxt.com/docs/getting-started/confetti-tutorial) for further examples.

[Edit this page](https://github.com/nuxt/scripts/edit/main/docs/content/npm.md)

[Markdown For LLMs](https://scripts.nuxt.com/scripts/npm.md)

[**Mixpanel** Load Mixpanel and track product events, identities, profiles, and consent.](https://scripts.nuxt.com/scripts/mixpanel-analytics) [**PayPal** Load PayPal's JavaScript SDK v6 and create checkout or messaging sessions from Vue components.](https://scripts.nuxt.com/scripts/paypal)

**On this page **

- [Background](#background)
- [useScriptNpm()](#usescriptnpm)
- [Example](#example)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://scripts.nuxt.com/#website","@type":"WebSite","description":"Nuxt Scripts lets you load third-party scripts with better performance, privacy, security and DX. It includes many popular third-parties out of the box.","name":"Nuxt Scripts","url":"https://scripts.nuxt.com/","publisher":{"@id":"https://scripts.nuxt.com/#identity"}},{"@id":"https://scripts.nuxt.com/scripts/npm#webpage","@type":"WebPage","description":"Load IIFE scripts from NPM in your Nuxt app.","name":"NPM","url":"https://scripts.nuxt.com/scripts/npm","breadcrumb":{"@id":"https://scripts.nuxt.com/scripts/npm#breadcrumb"},"isPartOf":{"@id":"https://scripts.nuxt.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://scripts.nuxt.com/scripts/npm"]}],"primaryImageOfPage":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/#identity","@type":"Organization","name":"Nuxt Scripts","url":"https://scripts.nuxt.com","logo":{"@id":"https://scripts.nuxt.com/#logo"}},{"@id":"https://scripts.nuxt.com/scripts/npm#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Nuxt Scripts","item":"https://scripts.nuxt.com/","position":1},{"@type":"ListItem","name":"Scripts","item":"https://scripts.nuxt.com/scripts","position":2},{"@type":"ListItem","name":"NPM","item":"https://scripts.nuxt.com/scripts/npm","position":3}]},{"@id":"https://scripts.nuxt.com/#logo","@type":"ImageObject","caption":"Nuxt Scripts","contentUrl":"https://scripts.nuxt.com/logo.svg","inLanguage":"en","url":"https://scripts.nuxt.com/logo.svg"}]}
```

## Sitemap

See the full [sitemap](https://scripts.nuxt.com/sitemap.md) for all pages.
