Getting the error "Error: Couldn't find preset "env" relative to directory"

Getting the error "Error: Couldn't find preset "env" 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

您的所有错误都发生在构建期间

尝试以下步骤修复构建期间发生的错误

  • 从您的 git 存储库下载您的项目并将其放入新文件夹

  • 删除node_modules目录

  • 使用npm install

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

  • 您已将 vue-style-loader 包含在您的 webpack.config.js 中。但是你还没有安装这个加载器。安装 vue-style-loader 以允许 webpack 解析 .vue 文件中编码的样式 npm install --save-dev vue-style-loader

  • 您目前"vue-loader": "^15.7.0"使用的vue-loader需要在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 以在 browser.Chrome 浏览器中查看输出是首选。我已经忽略了你的代码在节点控制台中产生的警告消息,你必须注意它。

Uglify 插件在构建生产项目时几乎没有问题

npm run build

我建议您从 webpack.config.js 中删除 uglifyjs 相关配置并在生产模式下测试您的代码。您可以继续研究如何使用 uglifyjs 插件解决问题或 post 一个新问题。

没有 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
    })
  ])
}