Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ncc pack and then pkg pack,got some error about node.path #1202

Open
xiatian12345 opened this issue Jul 11, 2024 · 0 comments
Open

ncc pack and then pkg pack,got some error about node.path #1202

xiatian12345 opened this issue Jul 11, 2024 · 0 comments

Comments

@xiatian12345
Copy link

(base) ➜ test-node-pkg-to-exe tree
.
├── config.json
├── dist
│ ├── config.json
│ └── index.js
├── main.js
└── package.json

2 directories, 5 files

config.json
{ "appname": "myapp" }

main.js
`
const fs = require("fs");
const path = require("path");
const axios = require("axios");

let cfgPath = path.join(__dirname, "config.json");
fs.readFile(cfgPath, "utf8", (err, data) => {
if (err) {
console.error("Error reading config file:", err);
process.exit(1);
}

const config = JSON.parse(data);
console.log("Config:", config);

axios
	.get("https://www.google.com")
	.then((response) => {
		console.log("Response:", response.data);
	})
	.catch((error) => {
		console.error("Error:", error);
	});

});

setInterval(() => {
console.log("isRunning...");
}, 1000);

`

and then i run the command--->ncc build main,it generate the dist dir,and the index.js last lines like this

`
const fs = nccwpck_require(147);
const path = nccwpck_require(17);
const axios = nccwpck_require(515);

let cfgPath = nccwpck_require.ab + "config.json";
fs.readFile(nccwpck_require.ab + "config.json", "utf8", (err, data) => {
if (err) {
console.error("Error reading config file:", err);
process.exit(1);
}

const config = JSON.parse(data);
console.log("Config:", config);

axios
	.get("https://www.google.com")
	.then((response) => {
		console.log("Response:", response.data);
	})
	.catch((error) => {
		console.error("Error:", error);
	});

});

setInterval(() => {
console.log("isRunning...");
}, 1000);

})();
`

the node of path is not use!!!!

when i use the pkg pack the index.js , pkg dist/index.js --targets node14-macos-x64,this is generate the index binary package,but when i run the package i got the error :Error reading config file: Error: File '/**/dist/config.json' was not included into executable at compilation stage. Please recompile adding it as asset or script.

and then i try to modify the dist/index.js

`
const fs = nccwpck_require(147);
const path = nccwpck_require(17);
const axios = nccwpck_require(515);

let cfgPath = path.join(__dirname, "config.json");
fs.readFile(cfgPath, "utf8", (err, data) => {
if (err) {
console.error("Error reading config file:", err);
process.exit(1);
}

const config = JSON.parse(data);
console.log("Config:", config);

axios
	.get("https://www.google.com")
	.then((response) => {
		console.log("Response:", response.data);
	})
	.catch((error) => {
		console.error("Error:", error);
	});

});

setInterval(() => {
console.log("isRunning...");
}, 1000);

`

and then use pkg pack the application:pkg dist/index.js --targets node14-macos-x64, i run the index is success !

so,this is a bug of ncc or pkg?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant