鍍金池/ 問答/HTML/ vue-cli npm run build報錯 ,resolve is not

vue-cli npm run build報錯 ,resolve is not defined

我在用webpack打包時,運(yùn)行npm run build,首先報了一個錯
'''ERROR in build.js from UglifyJs
Unexpected token: punc (() ./node_modules/element-ui/packages/col/src/col.js:24,0'''
根據(jù)搜索到的結(jié)果是es6語法問題
然后我把webpack改成了這樣
'''
var path = require('path')
var webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
entry: './src/main.js',
output: {

path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'

},
module: {

rules: [
  {
    test: /\.css$/,
    loader: 'style-loader!css-loader'
  },
  {
    test: /\.scss$/,
    use: [
      'vue-style-loader',
      'css-loader',
      'sass-loader'
    ],
  },
  {
    test: /\.sass$/,
    use: [
      'vue-style-loader',
      'css-loader',
      'sass-loader?indentedSytanx'
    ],
  },
  {
    test: /\.(eot|svg|ttf|woff|woff2)$/,
    loader: 'file-loader'
  },
  {
    test: /\.vue$/,
    loader: 'vue-loader',
    options: {
      loaders: {
        // Since sass-loader (weirdly) has SCSS as its default parse mode, we map
        // the "scss" and "sass" values for the lang attribute to the right configs here.
        // other preprocessors should work out of the box, no loader config like this necessary.
        'scss': [
          'vue-style-loader',
          'css-loader',
          'sass-loader'
        ],
        'sass': [
          'vue-style-loader',
          'css-loader',
          'sass-loader?indentedSyntax'
        ]
      }
      // other vue-loader options go here
    }
  },
  {
    test: /\.js$/,
    loader: 'babel-loader',
    include: [resolve('src'),resolve('test'), resolve('/node_modules/iview/src'), resolve('/node_modules/iview/packages')],
    exclude: /node_modules/
  },
  {
    test: /\.(png|jpg|gif|svg)$/,
    loader: 'file-loader',
    options: {
      name: '[name].[ext]?[hash]'
    }
  }
]

},
resolve: {

alias: {
  'vue$': 'vue/dist/vue.esm.js'
},
extensions: ['*', '.js', '.vue', '.json']

},
devServer: {

historyApiFallback: true,
noInfo: true,
overlay: true,
host: '0.0.0.0'

},
performance: {

hints: false

},
devtool: '#eval-source-map'
}

if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/e...
module.exports.plugins = (module.exports.plugins || []).concat([

new webpack.DefinePlugin({
  'process.env': {
    NODE_ENV: '"production"'
  }
}),
// new webpack.optimize.UglifyJsPlugin({
//   sourceMap: true,
//   compress: {
//     warnings: false
//   }
// }),
new UglifyJsPlugin({

  // 使用外部引入的新版本的js壓縮工具

  parallel: true,

  uglifyOptions: {
    ie8: false,
    ecma: 6,
    warnings: false,
    mangle: true,
    // debug false
    output: {
      comments: false,
      beautify: false,
      // debug true
    },

    compress: {
      // 在UglifyJs刪除沒有用到的代碼時不輸出警告
      warnings: false,
      // 刪除所有的 `console` 語句
      // 還可以兼容ie瀏覽器
      drop_console:
        true,
      // 內(nèi)嵌定義了但是只用到一次的變量
      collapse_vars:
        true,
      // 提取出出現(xiàn)多次但是沒有定義成變量去引用的靜態(tài)值
      reduce_vars:
        true,
    }
  }
}),
new webpack.LoaderOptionsPlugin({
  minimize: true
})

])
}

'''

再次運(yùn)行npm run build,結(jié)果報錯
'''
path: path.resolve(__dirname, './dist'),

           ^

ReferenceError: resolve is not defined

at Object.<anonymous> (C:\Users\yingyi\Desktop\yingyi\webpack.config.js:8:16)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
at Module.require (module.js:579:17)
at require (internal/module.js:11:18)
at requireConfig (C:\Users\yingyi\Desktop\yingyi\node_modules\webpack\bin\convert-argv.js:97:18)
at C:\Users\yingyi\Desktop\yingyi\node_modules\webpack\bin\convert-argv.js:104:17

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! yingyi@1.0.0 build: cross-env NODE_ENV=production webpack --progress --hide-modules
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the yingyi@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:UsersyingyiAppDataRoamingnpm-cache_logs2018-05-21T11_19_19_460Z-debug.log
'''
請問這是什么原因呢

回答
編輯回答
喵小咪

node什么版本的?可以運(yùn)行node -v 查看

2018年4月30日 02:20
編輯回答
傲寒

建議在 webpack.config.js 中打出 path 對象,看看這個對象有沒有 resolve 方法,一般是有的

clipboard.png

2018年5月9日 09:34