nwjs / grunt-nw-builder

Build NW.js applications for Mac, Windows and Linux using Grunt
https://www.npmjs.com/package/grunt-nw-builder
MIT License
793 stars 141 forks source link

"cannot find module" error on aws-sdk #53

Closed Soviut closed 10 years ago

Soviut commented 10 years ago

My build works in OSX but not Windows 7. I'm requiring aws-sdk and the final exe it's throwing a "cannot find module" error.

I'm using Grunt to set up a clean temporary folder containing my index.html files, package.json and node_module, then pointing nodewebkit at it. Like I said, this works on OSX but not Windows.

steffenmllr commented 10 years ago

Which versions of node are you using?

Soviut commented 10 years ago

v0.10.15

steffenmllr commented 10 years ago

Does the aws sdk have some native (C/C++) modules? Are you building on a mac oder windows?

Soviut commented 10 years ago

The aws-sdk module doesn't appear to have any native bindings; It's all just REST wrappers. When I build on my macbook I get a working binary/app, the AWS calls are made and it populates my Angular frontend. On Windows, it builds but it gets an error saying aws-sdk could not be found.

steffenmllr commented 10 years ago

Hmm that is weird. You know that you can build the win version on mac and vice versa? Is it opensouce software? can I have a look at the repo?

Soviut commented 10 years ago

I've never tried the windows build that I made on my mac. Unfortunately it's a private repo on bitbucket and I've got my aws keys in it so I can't make it public.

Here's my Gruntfile.coffee at least.

module.exports = (grunt) ->
    grunt.initConfig
        paths:
            app: 'app'
            scripts: '<%= paths.app %>/static/scripts'
            dist: 'dist'
            tmp: '.tmp'

        watch:
            jade:
                files: ['<%= paths.app %>/*.jade']
                tasks: ['jade']
            coffee:
                files: ['<%= paths.scripts %>/**/*.coffee']
                tasks: ['coffee']

        jade:
            options:
                pretty: true
            dist:
                files:
                    '<%= paths.app %>/index.html': '<%= paths.app %>/index.jade'

        coffee:
            dist:
                files: [
                    expand: true
                    cwd: '<%= paths.scripts %>'
                    src: '**/*.coffee'
                    dest: '<%= paths.scripts %>'
                    ext: '.js'
                ]

        clean:
            dist:
                src: [
                    '<%= paths.dist %>/releases'
                    '<%= paths.tmp %>'
                ]

        copy:
            dist:
                files: [
                    src: 'package.json'
                    dest: '<%= paths.tmp %>/package.json'
                ,
                    src: '<%= paths.app %>/index.html'
                    dest: '<%= paths.tmp %>/index.html'
                ,
                    expand: true
                    cwd: '<%= paths.app %>'
                    src: '**'
                    dest: '<%= paths.tmp %>'
                ,
                    expand: true
                    cwd: 'node_modules'
                    src: 'aws-sdk'
                    dest: '<%= paths.tmp %>/node_modules'
                ]

        nodewebkit:
            options:
                build_dir: '<%= paths.dist %>'
                keep_nw: true
                mac: true
                win: true
            dist:
                files: [
                    cwd: '<%= paths.tmp %>'
                    expand: true
                    src: '**/*'
                ]

    grunt.loadNpmTasks 'grunt-contrib-watch'
    grunt.loadNpmTasks 'grunt-contrib-jade'
    grunt.loadNpmTasks 'grunt-contrib-less'
    grunt.loadNpmTasks 'grunt-contrib-coffee'
    grunt.loadNpmTasks 'grunt-contrib-clean'
    grunt.loadNpmTasks 'grunt-contrib-copy'
    grunt.loadNpmTasks 'grunt-node-webkit-builder'

    grunt.registerTask 'default', ['compile', 'watch']
    grunt.registerTask 'compile', ['jade', 'coffee']
    grunt.registerTask 'publish', ['compile', 'clean', 'copy', 'nodewebkit']

Notice that my "publish" task compiles the jade and coffeescript, cleans out my .tmp and dist folders, then populates the .tmp folder with the package.json from my project root, the index.html and the node_modules/aws-sdk. Then nodewebkit is pointed at that to create the distribution.

steffenmllr commented 10 years ago

looks good, did you try running the .nw file as described here: https://github.com/rogerwang/node-webkit/wiki/How-to-run-apps#wiki-windows ?

btw. It's not a good ideas to save you aws credentials or any kind of sensetive information within a node webkit app. Since it is only a zip file you can easily open the mac.app folder and look at the code...

Soviut commented 10 years ago

This is an internal app that I'm building for the place where I work, the credentials will be invalid in a month so it doesn't matter.

I'll try running the nw file directly tomorrow.

steffenmllr commented 10 years ago

Tell me if it worked

Soviut commented 10 years ago

@steffenmllr I finally got a chance to try running the app directly on Windows using nw.exe. I'm getting the same Uncaught Error: Cannot find module 'aws-sdk'.

Soviut commented 10 years ago

I figured out the issue. My node_modules folder wasn't being copied over correctly because I neglected to add a wildcard to the end of the src. The following is what it should have been.

files: [
    expand: true
    cwd: 'node_modules'
    src: 'aws-sdk/**/*'
    dest: '<%= paths.tmp %>/node_modules'
]
Soviut commented 10 years ago

That said, I've actually been able to simplify the process even further. I removed the copying to a temp folder entirely and just fed the nodewebkit task the files directly:

        nodewebkit:
            options:
                build_dir: '<%= paths.dist %>'
                keep_nw: true
                mac: true
                win: true
            dist:
                files: [
                    src: 'package.json'
                ,
                    src: 'index.html'
                ,
                    expand: true
                    src: '<%= paths.static %>/**/*'
                ,
                    expand: true
                    src: 'bower_components/**/*'
                ,
                    expand: true
                    cwd: 'node_modules'
                    src: [
                        'aws-sdk/**/*'
                        'fs.extra/**/*'
                    ]
                    dest: 'node_modules'
                ]