【问题标题】:Getting the error "Error: Couldn't find preset "env" relative to directory"收到错误“错误:找不到相对于目录的预设“env””
【发布时间】:2019-08-15 18:58:21
【问题描述】:

当我运行我的项目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

【问题讨论】:

标签: vue.js npm webpack babeljs vue-cli


【解决方案1】:

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

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

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

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

  • 删除node_modules目录

  • 使用npm install安装所有模块

  • 在您的package.json 中,您已包含cross-env 脚本来启动您的项目。 不要全局安装。您必须将此库安装到项目本地。 当您将项目部署到任何服务器时,您需要此 cross-env 来启动您的项目。安装 cross-env 包作为产品依赖项。 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 "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
    })
  ])
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2018-12-15
    相关资源
    最近更新 更多