Dmytro Zarezenko
Dmytro Zarezenko

Reputation: 10686

How to include JS file from npm package to separate page in Nuxt.js

I have several npm packages with client-side scripts I want to include to separate pages of my Nuxt.js project. I have tried to do that with:

<script>
export default {
  head: {
    script: [
      { src: "gojs/release/go.js", type: "text/javascript" }
    ]
  },

  // ...
}
</script> 

but received 404 (Not Found) error. What is correct way to do that?

Upvotes: 2

Views: 1953

Answers (1)

tony19
tony19

Reputation: 138556

Normally, import-ing the module (e.g., with import go from 'gojs') and using it in your code are all you need to do for the build to include that dependency. No need to add headers to load the NPM module.

Example in MyFoo.vue:

<script>
import go from 'gojs'

export default {
  mounted() {
    const $ = go.GraphObject.make;
    const myDiagram =
      $(go.Diagram, "myDiagramDiv",
        {
          "undoManager.isEnabled": true
        });

    ...
  }
}
</script>

demo

Upvotes: 1

Related Questions