未捕获的 ReferenceError:require 未在 require('./bootstrap') Laravel Mix 处定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42969805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:38:48  来源:igfitidea点击:

Uncaught ReferenceError: require is not defined at require('./bootstrap') Laravel Mix

javascriptlaravelwebpackmix

提问by awebartisan

I have compiled my stylesheets and javascript files using Laravel Mix, code is given below:

我已经使用 Laravel Mix 编译了我的样式表和 javascript 文件,代码如下:

const { mix } = require('laravel-mix');

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

// mix.js('resources/assets/js/app.js', 'public/js')
//    .sass('resources/assets/sass/app.scss', 'public/css');

mix.babel([
    'public/js/plugins/loaders/pace.min.js',
    'resources/assets/js/app.js',
    'public/js/plugins/loaders/blockui.min.js',
    'public/js/plugins/ui/nicescroll.min.js',
    'public/js/plugins/ui/drilldown.js',
    'public/js/plugins/ui/fab.min.js',
    'public/js/plugins/forms/selects/select2.min.js',
    'public/js/plugins/forms/styling/uniform.min.js',
    'public/js/plugins/ui/moment/moment.min.js',
    'public/js/plugins/ui/fullcalendar/fullcalendar.min.js',
    'public/js/core/app.js',
    'public/js/pages/user_pages_profile.js',
] , 'public/js/app.js');

mix.styles([
    'public/css/icons/icomoon/styles.css',
    'resources/assets/sass/app.scss',
    'public/css/core.css',
    'public/css/components.css',
    'public/css/colors.css',
],'public/css/app.css');


Below is my app.js


下面是我的app.js

/**
 * First we will load all of this project's JavaScript dependencies which
 * includes Vue and other libraries. It is a great starting point when
 * building robust, powerful web applications using Vue and Laravel.
 */

require('./bootstrap'); //complains on this line after compilation

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});


It complains on the required,
Uncaught ReferenceError: require is not defined at app.js:354, this is the compiled app.js
I have tried:
1- mix.scripts
2- mix.babel(according to docs this will translate any ES2015 code to vanilla JavaScript that all browsers will understand.)
What am i missing here?


它抱怨required,
Uncaught ReferenceError: require is not defined at app.js:354,这是 我试过的编译后的app.js

1- mix.scripts
2- mix.babel(根据文档,这将翻译任何 ES2015所有浏览器都可以理解的香草 JavaScript 代码。)
我在这里错过了什么?

回答by Rob Fonseca

Your node version is most likely out of date. Node JS 6 was the first version to handle ES2015 modules. Check your Node version by running

您的节点版本很可能已过时。Node JS 6 是第一个处理 ES2015 模块的版本。通过运行检查您的节点版本

node -v

If you are using Ubuntu 16, you need to add a source repository to pull a newer version of node. Running the following commands should get you up to the latest version

如果您使用的是 Ubuntu 16,则需要添加源存储库以拉取较新版本的节点。运行以下命令应该可以让您升级到最新版本

cd ~
curl -sL https://deb.nodesource.com/setup_6.x -o nodesource_setup.sh
sudo bash nodesource_setup.sh
sudo apt-get install nodejs
sudo npm update -g npm 
sudo ln -s /usr/bin/nodejs /usr/bin/node

Now try running npm run devand everything should compile fine in Laravel mix.

现在尝试运行npm run dev,一切都应该在 Laravel mix 中编译正常。