我从使用nuxt generate
构建的nuxt网站上收到此错误
Uncaught ReferenceError: require is not defined
此代码在两个文件中发生:
```
vendor.js: e.exports=require("vue")
app.js: t.exports=require("core-js/library/fn/symbol/iterator")
```
当我仅用nuxt
预览站点时,它运行正常。
我遇到了this bug,所以我使用的是我不太了解的webpack-node-externals
。
```
const nodeExternals = require('webpack-node-externals');
module.exports = {
/* Headers of the page */
head: { },
css: [ ],
/* Build configuration */
build: {
extend (config, { isDev, isClient }) {
if (isDev && isClient) {
/* Run ESLint on save */
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
} else {
// https://github.com/nuxt/nuxt.js/issues/438
config.externals = [ nodeExternals({
whitelist: ['vue-videobg']
})]
}
}
},
modules: [
'nuxt-imagemin',
['nuxt-sass-resources-loader', [
'@/node_modules/bootstrap/scss/_functions.scss',
'@/node_modules/bootstrap/scss/_variables.scss',
'@/node_modules/bootstrap/scss/mixins/_breakpoints.scss',
'@/assets/bootstrap4/_variables.scss',
'@/assets/variables.scss',
]],
['bootstrap-vue/nuxt', { css: false, }],
['@nuxtjs/google-analytics', {
id: 'UA-XXXXXXXXX-6'
}],
],
plugins: [
'plugins/bootstrap-vue.js',
'plugins/scroll.js',
{ ssr: false, src: '~/plugins/startup.js', },
{ ssr: false, src: '~/plugins/ksvuescrollmagic.js', },
],
}
```
答案 0 :(得分:0)
嗯,这是风滚草。我使用isServer
修复了它:
if (isServer) {
// https://github.com/nuxt/nuxt.js/issues/438
config.externals = [ nodeExternals({
whitelist: ['vue-videobg']
})]
}
答案 1 :(得分:0)
如果您使用Nuxt v.2.0,则不再鼓励require。
这就是2.0的变更日志:
Upgrade nuxt.config.js
, modules
and serverMiddleware
to use import/export
instead of require