简体   繁体   English

我如何才能确保 nodejs pkg 包含我的所有文件? 可执行文件在根文件夹中运行,但不在其他任何地方

[英]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.所以我有一个项目是 html 和 javascript。 You click index.html and it opens a 2D simulation in the browser.您单击 index.html 并在浏览器中打开 2D 模拟。 I'd like to package this as a standalone executable (.exe).我想将 package 作为独立的可执行文件 (.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'.为了用节点触发这个,我简单地创建了一个执行'npm start'的app.js,它又执行'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.当 pkg ( https://www.npmjs.com/package/pkg )只是一个纯脚本时,我已经取得了巨大的成功,但是当我将此项目编译为 exe 时,它将在浏览器中打开并正常工作。 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 pkg. -t host . pkg. -t host

I've tried including all files with 'glob'(), like so:我试过用'glob'()包含所有文件,如下所示:

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').但这会返回有关 window 的错误(“未定义窗口”)。 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:编辑:这是文件夹结构和我的 package.json:

folder structure文件夹结构

package.json: 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.这就是 FTP 经常工作的方式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何随机化我的问题? 我在别处找不到答案 - How do I randomize my questions? I can't find an answer anywhere else 如何确保工作不会在 Bull 中运行两次? - How can I make sure that a job doesn't run twice in Bull? 如何确保通过CDN传送的JavaScript文件不会被更改? - How can I make sure that my JavaScript files delivered over a CDN are not altered? 如何确保我的twitch机器人只能使用NODEJS和TMI API接收某些用户的命令? - How can I make sure that my twitch bot can only take commands from certain users with NODEJS and the TMI API? 在将数据推送到数据库之前,如何确保 if 函数运行? - How do I make sure my if funtion runs before my data is pushed to a database? 如何确保我的 javascript 动画在提交按钮完成之前运行? - How do I make sure my javascript animation runs before submit button is done? 我如何发布一个参数,使其不出现在查询字符串中,以确保用户不能直接在URL中更改该值? - How can I POST a parameter so that it doesn't show up in the querystring, to make sure users can't change the value directly in the URL? 如何确保正确加载geojson文件? - How can I make sure that the geojson files are loaded properly? 如何确保我的函数每次点击只运行一次 - How to make sure my function runs only once per click 如何确保 function 只运行一次并触发多个绑定和事件? - How can I make sure a function only runs once with multiple bindings and events firing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM