npm安装不会创建dist文件夹。

12

我正在按照链接中的教程创建一个grafana插件。

但是,当我将链接中的代码(不包括dist/文件夹)复制到我的测试服务器,然后运行npm install时,npm并没有创建一个新的dist/文件夹,而是创建了一个node_modules文件夹。

我是否漏掉了什么步骤或者理解错了什么?因为我预期这个命令会从src/文件夹中的文件创建一个dist/文件夹?

grunt文件:

module.exports = (grunt) => {
  require('load-grunt-tasks')(grunt);

  grunt.loadNpmTasks('grunt-execute');
  grunt.loadNpmTasks('grunt-contrib-clean');

  grunt.initConfig({

    clean: ['dist'],

    copy: {
      src_to_dist: {
        cwd: 'src',
        expand: true,
        src: ['**/*', '!**/*.js', '!**/*.scss'],
        dest: 'dist'
      },
      pluginDef: {
        expand: true,
        src: [ 'plugin.json', 'README.md' ],
        dest: 'dist',
      }
    },

    watch: {
      rebuild_all: {
        files: ['src/**/*', 'plugin.json'],
        tasks: ['default'],
        options: {spawn: false}
      },
    },

    babel: {
      options: {
        sourceMap: true,
        presets: ['es2015'],
        plugins: ['transform-es2015-modules-systemjs', 'transform-es2015-for-of'],
      },
      dist: {
        files: [{
          cwd: 'src',
          expand: true,
          src: ['*.js'],
          dest: 'dist',
          ext: '.js'
        }]
      },
    },

  });

  grunt.registerTask('default', ['clean', 'copy:src_to_dist', 'copy:pluginDef', 'babel']);
};

package.json文件:

{
  "name": "clock-panel",
  "version": "1.0.0",
  "description": "Clock Panel Plugin for Grafana",
  "main": "src/module.js",
  "scripts": {
    "lint": "eslint --color .",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "clock",
    "grafana",
    "plugin",
    "panel"
  ],
  "author": "Raintank",
  "license": "MIT",
  "devDependencies": {
    "babel": "~6.5.1",
    "babel-eslint": "^6.0.0",
    "babel-plugin-transform-es2015-modules-systemjs": "^6.5.0",
    "babel-preset-es2015": "^6.5.0",
    "eslint": "^2.5.1",
    "eslint-config-airbnb": "^6.2.0",
    "eslint-plugin-import": "^1.4.0",
    "grunt": "~0.4.5",
    "grunt-babel": "~6.0.0",
    "grunt-contrib-clean": "~0.6.0",
    "grunt-contrib-copy": "~0.8.2",
    "grunt-contrib-uglify": "~0.11.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-execute": "~0.2.2",
    "grunt-systemjs-builder": "^0.2.5",
    "load-grunt-tasks": "~3.2.0"
  },
  "dependencies": {
    "lodash": "~4.0.0",
    "moment": "^2.12.0"
  }
}

不确定这会不会对任何人有所帮助,但我正在手动将文件从一个现有项目复制到一个新项目中,却忘记复制index.js,这个文件中包含了用于将代码打包到dist并使用serve-static进行静态文件服务的代码。我是在按照这个教程进行操作的。 - undefined
3个回答

8

您没有运行grunt默认任务

您应该运行:

npm install(安装您的依赖项),然后运行grunt(它将源文件复制到dist,如您在Gruntfile.js中看到的copy:src_to_dist任务)

简而言之,只需运行:$ npm install && grunt


1

npm install 命令会安装项目所需的依赖包。如果当前目录下不存在 node_modules 目录,它将在该目录下创建此目录,并将下载好的包保存到其中。


2
“npm install”与创建(或未能创建)“dist”文件夹无关,这正是OP所询问的。 - mrodo
@mrodo,实际上,有一个“prepublish”功能,有时用于将文件移动到dist文件夹中。 - bvdb

1

实际上,运行npm install将会在有package.json文件的情况下执行prepublish操作。

根据您的需求,您可能需要执行以下操作:

  "scripts": {
    "build": "grunt",
    "prepublish": "npm run build"
  },

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接