curist / grunt-bower

grunt task to copy bower installed packages to other folder(s)
MIT License
93 stars 27 forks source link

Got error: Cannot find module 'bower' #10

Closed zation closed 11 years ago

zation commented 11 years ago

I've install grunt-bower and config my Gruntfile.js as below:

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('component.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'components/backbone/backbone.js',
        dest: 'js/main.min.js'
      }
    },
    bower: {
      dev: {
        dest: 'dest/path'
      }
    }
  });

  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-bower');

  // Default task(s).
  grunt.registerTask('default', ['uglify', 'bower']);

};

but when I run grunt, I got Error: Cannot find module 'bower'. And I checked the package.json of grunt-bower, I noticed the bower is in devDependencies. Can you put it into dependencies?

curist commented 11 years ago

As stated in the readme file, grunt-bower assumes you have bower installed globally. Try npm install -g bower and see if it works.

curist commented 11 years ago

Personally, I want to handle the package management by command line, so a global installed bower is needed.

If you prefer a more streamlined workflow, take a look at grunt-bower-task.

nrako commented 11 years ago

FYI I tried and install of bower globally wont be enough it has to be installed locally npm install bower which is something I would like to avoid.

curist commented 11 years ago

@nrako can you be more specific on your setup? grunt version, bower version etc.

nrako commented 11 years ago

Latest version for everything, but I don't think the problem rely on the version at least if you use npm > 1.0.0. Check this link http://blog.nodejs.org/2011/03/23/npm-1-0-global-vs-local-installation/

curist commented 11 years ago

I tried several setups, grunt 0.3.7 or 0.4.0, always globally installed bower. Never have any issue you mentioned. I'm really trying to tackle this down, but have no idea what's can be wrong here. Do you need privilege to install bower globally? Something like sudo npm install -g bower? Do you have $NODE_PATH correctly set?

nrako commented 11 years ago

Ah! Okay of course now I understand, you probably have the global node_modules directory in your NODE_PATH.

If you try require.resolve('bower') it probably show up bower from your NODE_PATH.

My NODE_PATH is setup correctly which is empty. NODE_PATH exists mostly for legacy reason and it's highly discouraged to use it. There are many reasons for that, what happening now is one of them.

From nodejs.org

These are mostly for historic reasons. You are highly encouraged to place your dependencies locally in node_modules folders. They will be loaded faster, and more reliably.

curist commented 11 years ago

I'm not aware of that NODE_PATH is a legacy environment variable, good to learn that!! Just unset my NODE_PATH and use npm config set prefix MY_NODE_MODULE_PATH, everything work just fine.

But, I can't really see how this is related how you require a globally installed package. If setup correctly, no matter via $prefix or $NODE_PATH, require('some_package') should just works.

nrako commented 11 years ago

No It shouldn't global package are for CLI. If a package is used both as a cli and a local package require()it must be installed both globally and locally, or maybe use npm link (but I wouldn't except for rare case). I learnt something too with npm config set prefix I don't really understand how it solve this but if I were you, I wouldn't change anything, you shouldn't have to.

npm folder — ref first bullet point list.

Maybe you think it is a bit anti-DRY but that just the way npm works, all dependencies of your package should be listed in your package.json.

BTW Grunt adopted an elegant solution to do that (among others) with grunt-cli (-g) and grunt local package where grunt-cli is only a cli interface for the local grunt package, maybe bower will come to that in the future.

curist commented 11 years ago

Just added bower as dependency to package.json. Thanks for your time sharing this, much appreciated.