使用Grunt-browserify构建npm webshot模块时出错

时间:2014-06-26 23:16:07

标签: node.js gruntjs npm browserify

我从这里安装了webshot:

https://www.npmjs.org/package/webshot

使用:npm install webshot

然后我添加'var webshot = require('webshot');'到js文件,然后运行:

grunt build

通常它会将所有npm模块编译成一个lib.js文件,我可以在我的项目中包含在前端使用npm模块。

但我收到错误:

>> Error: module "os" not found from "/Users/Feroze/Documents/meteor_apps/Bookmark_Website/.npm/node_modules/webshot/node_modules/tmp/lib/tmp.js"
File written to: ../lib/bundle.js
>> Error: module "constants" not found from "/Users/Feroze/Documents/meteor_apps/Bookmark_Website/.npm/node_modules/webshot/node_modules/tmp/lib/tmp.js"
File written to: ../lib/bundle.js

运行grunt构建时。为什么我无法使用npm webshot模块构建我的lib.js文件?一旦我使用npm卸载webshot,它就可以很好地构建lib.js文件。

Gruntfile.coffee

module.exports = function(grunt) {
  grunt.initConfig({
    watch: {
      build: {
          files: ['./entrypoint.js', './package.json'],
          tasks: ['browserify2'],
          options: {
          }
      }
    },
    browserify2: {
      compile: {
        entry: './entrypoint.js',
        compile: '../lib/bundle.js'
      }
    },
  });

  grunt.loadNpmTasks('grunt-browserify2');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('build', ['browserify2']);
};

的package.json

{
  "name": "Template",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.3.3",
    "grunt-contrib-uglify": "~0.4.0",
    "browserify": "^4.1.9",
    "grunt-browserify": "^2.1.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-webshot": "^0.3.0"
  },
  "dependencies": {
    "curtsy": "0.0.1"
  }
}

entrypoint.js

async = require('async')
webshot = require('webshot')

1 个答案:

答案 0 :(得分:1)

问题似乎源于使用grunt-browserify2而不是grunt-browserify," 2"版本似乎已过时,可能并未包含browserify的最新更新。尝试使用这些package.jsonGruntfile.js

<强>的package.json

{
  "name": "Template",
  "version": "0.1.0",
  "devDependencies": {
    "grunt": "~0.4.5",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.3.3",
    "grunt-contrib-uglify": "~0.4.0",
    "browserify": "^4.1.9",
    "grunt-browserify": "^2.1.0",
    "grunt-contrib-watch": "^0.6.1",
    "async": "^0.9.0",
    "webshot": "^0.15.0",
    "graceful-fs": "^3.0.2"
  },
  "dependencies": {
    "curtsy": "0.0.1"
  }
}

<强> Gruntfile.js

module.exports = function(grunt) {
  grunt.initConfig({
    watch: {
      build: {
          files: ['./entrypoint.js', './package.json'],
          tasks: ['browserify'],
          options: {
          }
      }
    },
    browserify: {
      vendor: {
        src: './entrypoint.js',
        dest: '../lib/bundle.js'
      }
    },
  });

  grunt.loadNpmTasks('grunt-browserify');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.registerTask('build', ['browserify']);
};