Vue单文件引入、webpack的配置及错误处理

 1. 安装vue:

cnpm i vue -S

2. 安装vue-loader、vue-template-compiler:

cnpm i vue-loader@14.1.1 vue-template-compiler@2.5.17 -D

提示:peerDependencies WARNING vue-loader@14.1.1 requires a peer of css-loader@* but none was installed。按照提示安装css-loader及其依赖的style-loader:

cnpm install css-loader@0.28.11 style-loader@0.23.1 -D

3. 执行:npm run build,报错(1):

nternal/modules/cjs/loader.js:968
  throw err;
  ^

Error: Cannot find module 'array-includes'
Require stack:
- E:\kaikevue\04single-file\node_modules\_webpack-dev-server@2.10.0@webpack-dev-server\lib\polyfills.js
- E:\kaikevue\04single-file\node_modules\_webpack-dev-server@2.10.0@webpack-dev-server\bin\webpack-dev-server.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:965:15)
    at Function.Module._load (internal/modules/cjs/loader.js:841:27)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (E:\kaikevue\04single-file\node_modules\_webpack-dev-server@2.10.0@webpack-dev-server\lib\polyfills.js:6:18)
    at Module._compile (internal/modules/cjs/loader.js:1137:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
    at Module.load (internal/modules/cjs/loader.js:985:32)
    at Function.Module._load (internal/modules/cjs/loader.js:878:14)
    at Module.require (internal/modules/cjs/loader.js:1025:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'E:\\kaikevue\\04single-file\\node_modules\\_webpack-dev-server@2.10.0@webpack-dev-server\\lib\\polyfills.js',
    'E:\\kaikevue\\04single-file\\node_modules\\_webpack-dev-server@2.10.0@webpack-dev-server\\bin\\webpack-dev-server.js'
  ]
}
npm ERR! code 1
npm ERR! path E:\kaikevue\04single-file
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c webpack-dev-server --open --hot --inline

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dell\AppData\Local\npm-cache\_logs\2021-09-23T07_39_07_998Z-debug.log

(1)解决办法:

删除项目中node_modules文件夹 ,

执行 npm cache clean 命令清除掉cache缓存,

npm install 重新安装依赖后,npm run build运行项目。

我在执行 npm cache clean的时候又报错(2):

npm ERR! As of npm@5, the npm cache self-heals from corruption issues
npm ERR! by treating integrity mismatches as cache misses.  As a result,
npm ERR! data extracted from the cache is guaranteed to be valid.  If you
npm ERR! want to make sure everything is consistent, use `npm cache verify`
npm ERR! instead.  Deleting the cache can only make npm go slower, and is
npm ERR! not likely to correct any problems you may be encountering!
npm ERR!
npm ERR! On the other hand, if you're debugging an issue with the installer,
npm ERR! or race conditions that depend on the timing of writing to an empty
npm ERR! cache, you can use `npm install --cache /tmp/empty-cache` to use a
npm ERR! temporary cache instead of nuking the actual one.
npm ERR!
npm ERR! If you're sure you want to delete the entire cache, rerun this command
npm ERR! with --force.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\dell\AppData\Local\npm-cache\_logs\2021-09-23T07_59_18_134Z-debug.log

(2)解决办法:

执行:

npm cache clean --force

或者:

npm cache clear --force && npm install --no-shrinkwrap --update-binary

最后执行npm run build,成功编译。

4. 目录及文件代码

Vue单文件引入、webpack的配置及错误处理

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app"></div>

</body>
</html>

 main.js:

// npm i vue的引入
import Vue from 'vue';
import App from './App.vue';

new Vue({
  el: '#app',
  //Render函数:使用虚拟dom来渲染节点提升性能,因为它是基于JavaScript计算.
  //通过使用createElement(h)来创建dom节点,createElement是render的核心方法.其Vue编译的时候会把template里面的节点解析成虚拟dom;
  render: c => c(App)
});

App.vue:

<template>
  <!-- 模板 -->
  <div> <!-- 只能有一个root -->
    {{ msg }}
  </div>
</template>

<script>
// 逻辑
 export default {
   data () {
     return {
       msg: 'hello Vue'
     }
   }
 }
</script>

<style>
/* 样式 */
  body {
    background-color: cyan;
  }
</style>

 webpack.config.js:

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  // 入口文件
  entry: {
    'main': './src/main.js'
  },
  // 出口文件
  output: {
    path: path.resolve('./dist'), //相对转绝对
    filename: 'build.js'
  },
  // 模块
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      },
      {
        //处理 es6,7,8
        test: /\.js$/,
        loader: 'babel-loader',
        // 排除node_modules下js文件的转换
        exclude: /node_modules/,
        options: {
          presets: 'env', // 处理关键字
          plugins: 'transform-runtime'  // 处理函数
        }
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      }
    ]
  },
  // 插件
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/index.html' // 参照物、模板
    })
  ]
}

 package.json:

{
  "name": "03es6",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack-dev-server --open --hot --inline",
    "dev": "webpack"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-preset-env": "^1.7.0",
    "css-loader": "^0.28.11",
    "html-webpack-plugin": "^2.24.1",
    "style-loader": "^0.23.1",
    "vue": "^2.6.14",
    "vue-loader": "^14.1.1",
    "vue-template-compiler": "^2.5.17",
    "webpack": "^3.12.0",
    "webpack-dev-server": "^2.10.0"
  },
  "dependencies": {
    "vue": "^2.6.14"
  }
}

 注意事项:

在package.json中的脚本,使用webpack-dev-server的脚本执行后,相应的代码会读进内存,不会在当前目录下产生dist文件夹。

Vue单文件引入、webpack的配置及错误处理

 

上一篇:40% building 31/38 modules 7 active ...es\core-js\modules\es6.object.assign.jsBrowserslist: caniu


下一篇:linux 安装 swftools 报错