获取错误"Error: Couldn't find preset "环境" relative to directory"



当我运行项目npm run dev or npm run build时收到此错误消息Error: Couldn't find preset "env" relative to directory

真的很感激一些帮助,两天来一直在尝试解决这个问题。

Error Log
ERROR in ./src/main.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Couldn't find preset "env" relative to directory "/Users/marvinbenno/Documents/Code projects/the-ultimate-website"
at /Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/options/option-manager.js:293:19
at Array.map (<anonymous>)
at OptionManager.resolvePresets (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/options/option-manager.js:275:20)
at OptionManager.mergePresets (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/options/option-manager.js:264:10)
at OptionManager.mergeOptions (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/options/option-manager.js:249:14)
at OptionManager.init (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/options/option-manager.js:368:12)
at File.initOptions (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/index.js:212:65)
at new File (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/file/index.js:135:24)
at Pipeline.transform (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-core/lib/transformation/pipeline.js:46:16)
at transpile (/Users/marvinbenno/Documents/Code projects/the-ultimate-website/the-ultimate-website-sneaekpeak/node_modules/babel-loader/lib/index.js:50:20)
@ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src/main.js main[2]"
./webpack.config.js
var path = require('path')
var webpack = require('webpack')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},      {
test: /.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: /.js$/,
loader: 'babel-loader',
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
},
performance: {
hints: false
},
devtool: '#eval-source-map'
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
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 webpack.LoaderOptionsPlugin({
minimize: true
})
])
}
./package.json
{
"name": "the-ultimate-website",
"description": "A Vue.js project",
"version": "1.0.0",
"author": "Marvin <marvinkristusbenno@gmail.com>",
"license": "MIT",
"private": true,
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
},
"dependencies": {
"@babel/polyfill": "^7.2.5",
"babel": "^6.23.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-3": "^6.24.1",
"bootstrap-vue": "^2.0.0-rc.14",
"joi": "^14.3.1",
"jshint-loader": "^0.8.4",
"vee-validate": "^2.2.0",
"vue": "^2.5.11",
"vue-resource": "^1.5.1",
"vue-router": "^3.0.2"
},
"browserslist": [
"> 1%",
"last 2 versions",
"not ie <= 8"
],
"devDependencies": {
"cross-env": "^5.0.5",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"vue-loader": "^15.7.0",
"vue-template-compiler": "^2.4.4",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
}
}

项目代码

https://github.com/Marre1020/the-ultimate-website-sneakpeak

首先,你不应该node_modules/目录签入 git 存储库。确保正确使用.gitignore

所有错误都在构建时发生

请尝试以下步骤来修复构建时发生的错误

  • 从您的 gitrepo 下载您的项目并将其放入新文件夹中

  • 删除node_modules目录

  • 使用npm install安装所有模块

  • 在您的package.json中,您已经包含cross-env脚本来启动项目。 不要全局安装它。您必须将此库安装到项目的本地。 将项目部署到任何服务器时,需要此cross-env来启动项目。cross-env包安装为生产依赖项。npm install --save cross-env

  • 您已在webpack.config.js中包含vue-style-loader。但是您尚未安装此加载程序。安装vue-style-loader以允许 webpack 解析.vue文件中编码的样式npm install --save-dev vue-style-loader

  • 您当前使用的vue-loader"vue-loader": "^15.7.0"需要在webpack.config.js中配置插件。在devtool configuration后包括VueLoaderPlugin,如下所示。

./webpack.config.js
...
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
...
devtool: '#eval-source-map',
plugins : [
new VueLoaderPlugin()
]
}
  • 在开发模式下运行项目npm run dev以在浏览器中查看输出。Chrome浏览器是首选。我已经忽略了您的代码在节点控制台中生成的警告消息,您必须处理它。

Uglify 插件在构建生产项目时几乎不会引起问题

npm run build

我建议您从webpack.config.js中删除与 uglifyjs 相关的配置,并在生产模式下测试您的代码。您可以继续研究更多有关如何解决uglifyjs插件问题或发布新问题的信息。

没有 uglifyjs 插件的 Webpack 配置

var path = require('path')
var webpack = require('webpack')
const { VueLoaderPlugin } = require('vue-loader')
module.exports = {
entry: './src/main.js',
output: {
path: path.resolve(__dirname, './dist'),
publicPath: '/dist/',
filename: 'build.js'
},
module: {
rules: [
{
test: /.css$/,
use: [
'vue-style-loader',
'css-loader'
],
},      {
test: /.vue$/,
loader: 'vue-loader',
options: {
loaders: {
}
// other vue-loader options go here
}
},
{
test: /.js$/,
loader: ['babel-loader'],
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
},
performance: {
hints: false
},
devtool: '#eval-source-map',
plugins : [
new VueLoaderPlugin()
]
}
if (process.env.NODE_ENV === 'production') {
module.exports.devtool = '#source-map'
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"'
}
}),
new webpack.LoaderOptionsPlugin({
minimize: true
})
])
}

最新更新