简体   繁体   中英

How can I just make sure that nodejs pkg is including all of my files? Executable runs in root folder but doesn't anywhere else

So I have a project that is html and javascript. You click index.html and it opens a 2D simulation in the browser. I'd like to package this as a standalone executable (.exe). I know there are other methods before I get suggestions but I want to do this with node, if possible.

To trigger this with node I've simply created an app.js that executes 'npm start', which in turn executes 'start index.html'.

I've had great success with pkg ( https://www.npmjs.com/package/pkg ) when its just a pure script but when I compile this project as an exe it WILL open in the browser and work fine. Issue is if I then move the executable from the original folder (eg to the desktop) it no longer works. I imagine this is because it is not including all of the files I need.

So far I have tried the following:

I compile with pkg. -t host pkg. -t host .

I've tried including all files with 'glob'(), like so:

var glob = require( 'glob' )
    , path = require( 'path' );

glob.sync( './html5/**/**/*.js' ).forEach( function( file ) {
    require( path.resolve( file ) );
});

glob.sync( './html5/**/**/*.css' ).forEach( function( file ) {
    require( path.resolve( file ) );
});

But this returns an error regarding window ('window is not defined'). I worked around this by adding:

if (typeof window !== "undefined") {
    window.globalProvideData('stuff', 'more stuff');
}

every time I got the error but eventually it was giving me errors I can't figure out.

So, how can I include all files so it behaves as it does while in the root folder?

EDIT: Here's the folder structure and my package.json:

folder structure

package.json:

{
  "name": "test_story",
  "version": "1.0.0",
  "description": "testing story exe",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "start story.html"
  },
  "bin": "app.js",
  "author": "",
  "license": "ISC",
  "dependencies": {
    "glob": "^7.1.6",
    "pkg": "^4.5.1"
  },
  "pkg": {
    "assets": [
      "html5/**/*",
      "mobile/*.jpg",
      "node_modules/**/*",
      "story_content/**/*"
    ]
  }
}

One simple trick to avoid the root folder would be to run batch files that launch your directories for you. Also, to insure all files have been transferred correctly you could check the folder size and compare. This is how FTP often works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM