javascript - Grunt/Babel 错误 : Unable to write "dist" file (Error code: EISDIR)

标签 javascript node.js gruntjs grunt-babel

我是 Grunt 及其所有插件的新手,但我想学习和设置一些很棒的前端工具。话虽如此,我一直在关注Grunt docs以及 Grunt 和 Babel 的一些类似问题,通过 Github ,但我似乎不断收到以下回溯:

Running "babel:dist" (babel) task
Warning: Unable to write "dist" file (Error code: EISDIR). Use --force to continue.

Aborted due to warnings.

任何对这个新手更清晰的解释将不胜感激。我是 JS 编程和设置 DevOps 的新手,因此鼓励提供一些有用的提示和最佳实践。

<小时/>

设置:

js_practice/(根项目)
|----package.json

所有发行版和软件包
|---node_modules/

服务器端( Node )支持
|---es6/
| ------ test.js
|---距离/

客户端(浏览器)支持
|----公共(public)/
|---es6/(位于公共(public)文件夹内)
| ----- test2.js
|---dist/(位于公共(public)文件夹内)

这是我当前的代码:

Grunt file.js

module.exports = function(grunt) {
        // All of your Grunt code must be specified inside this function!
        // Setup configuration...
    // Load tasks...
    // Define tasks...

    grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
            babel: {
                     options: {
                             sourceMap: true,
                             presets: ['babel-preset-es2015']
                     },
                     dist: {
                             files: [
                                 // Node source
                                 {
                                 src: ['es6/**/*.js'],
                                 dest: 'dist'},
                                 // Browser source
                                {
                                    src: ['public/es6/**/*.js'],
                                    dest: 'public/dist'},
                        ],
                    },
             },

            browserify: {
           dist: {
             options: {
               transform: [["babelify", { "stage": 0 }]]
             },
             files: {
               "build/bundle.js": "src/main.js"
             }
           }
             },

        jshint: {
            scripts: {
                src: ['scripts/**.js', 'lib/**.js']
            },

            tests: { // We can have more than one jshint task, this ones called `jshint:tests`
                src: 'tests/**.js'
            }
        },

                uglify: {
              options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
              },
              build: {
                src: 'src/<%= pkg.name %>.js',
                dest: 'build/<%= pkg.name %>.min.js'
              },
            scripts: {
                expand: true,
                cwd: 'scripts/',
                src: '**.js',
                dest: 'build/',
                ext: '.min.js'
            }
        },

        watch: {
                    scripts: {
                    files: ['**/*.js'],
                    tasks: ['jshint'],
                        },

            styles: {
                files: 'styles/**.less',
                task: 'less:styles'
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-browserify');
    grunt.loadNpmTasks('grunt-babel');

    grunt.registerTask('default', ['babel','browserify','jshint']);
    grunt.registerTask('build', ['jshint', 'uglify']);

};

最佳答案

只是扩展我在评论中所说的内容

Any clearer explanation to this newbie would be HIGHLY appreciated.

Nodejs 正在尝试写入一个名为 dist 的文件,但会产生错误,因为存在具有该名称的目录。

这个问题的原因可以在babel任务中找到。

  files: [{
      src: ['es6/**/*.js'],
      dest: 'dist'
    },
    // Browser source
    {
      src: ['public/es6/**/*.js'],
      dest: 'public/dist'
    }]

你必须告诉 Babel 获取 es6/ 中的每个文件,对其进行转换并将新文件放置在 dist/ 中文件夹。就目前而言,变压器尝试创建一个名为 dist.txt 的文件。将其重写为

  files: [{
      expand: true,
      cwd: 'es6/'
      src: ['**/*.js'],
      dest: 'dist/'
    },
    // Browser source
    {
      expand: true,
      cwd: 'public/es6/'
      src: ['**/*.js'],
      dest: 'public/dist/'
    }]

应该会产生更好的结果,但可以尝试一下。正如我提到的,我已经有一段时间没有使用 Grunt 了。 Take a look at the documentation on building the files object

就像我在评论中所说的那样,使用 jshint 或其他此类工具(eslint 是所有炒作......)来保持您自己的代码整洁。不要浪费时间对您可能不想自行修复的 lib 文件运行 jshint 任务。并且始终在源文件上运行 jshint,而不是由 babel 或 browserify 转换的文件。它只是一个帮助编写更好代码的工具,而不是一些生成器。

关于javascript - Grunt/Babel 错误 : Unable to write "dist" file (Error code: EISDIR),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36071303/

相关文章:

javascript - 使用 PHP 和 AJAX 更新 mySQL 中的 Google map 标记位置值

javascript - Cloudinary 文件上传错误

javascript - 从模块函数内访问调用者脚本的 __filename

javascript - 如果文件更改,如何自动增加 javascript 版本?

javascript - 使用 debug-brk 启动 Sails 不会运行 Grunt?

javascript - 使用 Grunt + Git 为 JavaScript 项目构建增量更新的工作流程

javascript - 如何仅为 Reactjs 中的任何组件导入 css

javascript - 表单未使用 BootStrap 验证必填字段

node.js - 将 Azure CosmosDB DocumentDB API 与 Graph API 结合使用

javascript - 适用于 Node 的 Azure SDK - key 保管库管理 - CORS 预检 : server responded with a status of 400 (Bad Request)