Fixing Uncaught ReferenceError: Vue is not defined with webpack - 小众知识

Fixing Uncaught ReferenceError: Vue is not defined with webpack

2022-07-06 14:39:05 苏内容
  标签: Vue/webpack
阅读:4429

I am moving to a workflow using WebPack, which bundles code. While transitioning, I stumbled across this error:

Uncaught ReferenceError: Vue is not defined

at eval (external “Vue”?548a:1)

at Object.<anonymous> (pcp_mainbundle.js:172)

at __webpack_require__ (pcp_mainbundle.js:20)

at eval (pcpVue.js?6cb8:1)

at Object.<anonymous> (pcp_mainbundle.js:196)

at __webpack_require__ (pcp_mainbundle.js:20)

at eval (pcpMain.js?814e:1)

at Object.<anonymous> (pcp_mainbundle.js:189)

at __webpack_require__ (pcp_mainbundle.js:20)

at Object.<anonymous> (pcp_mainbundle.js:181)

10000000111a2334:47 Uncaught TypeError: window.pcpRun is not a function

at 10000000111a2334:47

I ensured that the vue package is installed from npm:

npm install vue –save

(these are two dashes before save).

Then I imported it like this:

import Vue from ‘vue’;

as I was used to with other imports, and indeed as I have seen it in a very good Udemy course by Maximilian Schwarzmüller.

The reason it does not work is probably because this Udemy course is set up by vue-cli, which sets up package.json.

I did this manually / chose a different path.

In effect I suspect that ‘vue’ is being redefined there, to vue/dist/vue.esm.js

I got the hint here: Webpack Intro into Vue.js

resolve: { alias: { // Ensure the right Vue build is used ‘vue$’: ‘vue/dist/vue.esm.js’ } }

Therefore I could possibly update my webpack.config.js.

I chose to simply put it into my import, as this Forum thread suggests:

import Vue from ‘vue/dist/vue.esm.js’;

This now works for me, the error Uncaught ReferenceError: Vue is not defined is gone.

Update 2.3.2020 (solution): externals

As mentioned, I was transitioning from manual script tag includes to a webpack process. Previously, therefore, I had included Vue using a script tag.

In order for this to work, and Vue not being loaded TWICE, I had applied the following workaround in webpack.config.js:

externals: {

vue: ‘Vue’

},

这段感觉并没有起作用

this lead to webpack NOT including vue –> whenever it was included, webpack assumed that I would provide it externally, which I did not do.

The solution is, therefore, to remove this externals section from webpack.config.js


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1