使用node_modules / .bin / webpack时,无法读取undefined(mix.initialize())的属性

时间:2017-07-10 12:11:17

标签: laravel webpack laravel-mix

我尝试使用node_modules/.bin/webpack进行编译,我收到此错误:

Mix.initialize();       
   ^       
TypeError: Cannot read property 'initialize' of undefined      
at Object.<anonymous> (/home/vagrant/Code/stream/webpack.config.js:9:4)            
at Module._compile (module.js:570:32)      
at Object.Module._extensions..js (module.js:579:10)      
at Module.load (module.js:487:32)  
at tryModuleLoad (module.js:446:12)      
at Function.Module._load (module.js:438:3)       
at Module.require (module.js:497:17)      
at require (internal/module.js:20:19)      
at requireConfig 
(/home/vagrant/Code/stream/node_modules/webpack/bin/convert-argv.js:97:18)     
at /home/vagrant/Code/stream/node_modules/webpack/bin/convert-argv.js:104:17    

webpack.mix.js

let mix = require('laravel-mix').mix;       
mix.js('resources/assets/js/app.js', 'public/js')      
.sass('resources/assets/sass/app.scss', 'public/css');    

webpack.config.js

var path = require('path');       
var webpack = require('webpack');      
var Mix = require('laravel-mix').config;    
var plugins = require('laravel-mix').plugins;    
Mix.initialize();

我关注此视频:https://laracasts.com/series/learn-vue-2-step-by-step/episodes/26?autoplay=true

在03:29崩溃,我真的很感激任何帮助。

3 个答案:

答案 0 :(得分:2)

我正在关注同一个视频。以下是我为解决同一错误而采取的步骤:

  1. $ laravel new stream --dev
  2. $ cd stream
  3. $ npm install
  4. $ npm install babel-loader sass-loader vue-loader --save-dev
  5. $ cp node_modules/laravel-mix/setup/webpack.config.js ./
  6. 打开./webpack.config.js并使它看起来像这样:

    require('./node_modules/laravel-mix/src/index');
    require(Mix.paths.mix());
    Mix.dispatch('init', Mix);
    let WebpackConfig = require('./node_modules/laravel-mix/src/builder/WebpackConfig');
    module.exports = new WebpackConfig().build();
    
  7. $ node_modules/.bin/webpack

  8. 在视频
  9. 中以1:32左右拿起

    我也在视频中遇到更多问题。如果我找到那些解决方案,我会更新这个答案。

答案 1 :(得分:1)

In the latest version of laravel-mix you don't need require mix property. According library documentation you just need in webpack.mix.js file:

let mix = require('laravel-mix');

Also in package.json in section scripts you should have path to your vendor file with laravel-mix (if you don't need custom config), for example:

  "scripts": {
     "dev": "webpack --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
  },

答案 2 :(得分:1)

好吧,我终于做到了。

在视频中,他在开头删除了一些package.json包。 (包括laravel-mix)然后安装laravel-mix 0.3版本。

因此,当我创建laravel项目时,我不删除laravel-mix(版本为“0. *”。如果我这样做并且我运行npm run dev总是很好。

我无法理解为什么他之前删除包并安装它,并且

cp -r node_modules/laravel-mix/setup/** ./

希望对某人有用。