PKG Failed to make Bytecode - javascript

I am getting this warning when I try and run .. pkg index.js -t macOS
node v17.3.1
pkg#5.5.2
Warning Failed to make bytecode node17-arm64 for file /snapshot/______/index.js
was hoping anyone could help,
I have also tried to use -b and got
Error: ENOENT: no such file or directory, open '/var/folders/fy/c5tgsjcj63q73kfvg_dd53fh0000gn/T/pkg.d5ef9dd92b18360a4ff95824/node/out/Release/node
thank you

My Script was written in ES6, I ran it threw Babel and then tried again and it worked perfectly!!

I used #vercel/ncc to generate one index.js file and then use pkg on the generated index.js file.
My package.json scripts is like this:
"scripts": {
"test": "jest",
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"build": "ncc build -o build/lib ./src/index.js && pkg --target node16-win-x64 -o build/MY-API.exe ./build/lib/index.js"
},

Related

How to run npm script (package.json) written in an external js file?

I know it has been covered in different questions but mine is a bit different:
Sorry in advance if it sounds really noob.
this is the script in package.json:
"start": "nodemon ./index.js --exec \"node -r babel-register\"",
I replaced that with:
"start": "node scripts/start.js",
and in start.js, I do:
const { execSync } = require('child_process')
execSync('nodemon ../index.js --exec \"node -r babel-register\"')
which throws an error:
/bin/sh: nodemon: command not found
Am I right with "execSync"?
I tried import nodemon in the file but it is obviously not helping.
What you're doing should work if nodemon is installed globally, i.e. with:
npm install -g nodemon
But if it's installed as a project dependency, i.e. with:
npm install --save-dev nodemon
Then you'll need to run it from the directory containing all the locally installed binaries: node_modules/.bin/
So something like this should work:
execSync('./node_modules/.bin/nodemon ../index.js --exec \"node -r babel-register\"')

nodemon not loading environment variables

The following is in my gulpfile.js:
const nodemon = require('nodemon')
const server = nodemon({
script: './src/index.js',
ext: 'hbs json js css',
require: 'dotenv/config',
dotenv_config_path: './config/.env',
stdout: false // without this line the stdout event won't fire
})
I am looking to duplicate the exact same behavior as the dev script in the package.json file:
"scripts": {
"test": "jest --watch",
"dev": "nodemon -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css",
"debug": "nodemon --inspect -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css"
},
For one reason or another it seems that gulp is not registering the environment variables defined in the .env file. Although the line below works just fine when ran with npm run dev:
nodemon -r dotenv/config ./src/index.js dotenv_config_path=./config/.env -e js,hbs,json,css
You may need to add export to your environment vars and load them before running nodemon:
vars.env
export ENV_FIRST="J"
export ENV_LAST="Doe"
package.json
"scripts": {
"dev": "source vars.env; nodemon ..."
...
}

Cannot find module '../lib/cli' error when running server

I am trying to run build a small express API. The problem is I am having problems trying to get the server up and running. When I type npm start I get the following error.
> nodemon ./index.js --exec babel-node -e js
module.js:557
throw err;
^
Error: Cannot find module '../lib/cli'
The script I have written in the in that package.json is the following.
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start":"nodemon ./index.js --exec babel-node -e js"
},
Any help would be appreciated.
I needed to reinstall the node_modules.
cd your_project_folder
rm -rf node_modules
npm install
There can be some issue with the node_modules. You must check your node_modules installation globally as well as locally inside project directory.

How can I use Babel for CLI program?

I'm trying to write some CLI program on node using Babel. I'd seen question How do I use babel in a node CLI program? and there loganfsmyth said:
Ideally you'd precompile before distributing your package.
Okay, now I'm using:
"scripts": {
"transpile": "babel cli.js --out-file cli.es5.js",
"prepublish": "npm run transpile",
}
But, I faced the problem, when Babel adds 'use strict'; line right behind #!/usr/bin/env node header. For example, if I have cli.js:
#!/usr/bin/env node
import pkg from './package'
console.log(pkg.version);
I'll get this:
#!/usr/bin/env node'use strict';
var _package = require('./package');
… … …
And this doesn't work. When I try to run it, I always get:
/usr/bin/env: node'use strict';: This file or directory does'nt exist
How can I solved this problem?
The solution from #DanPrince is perfectly acceptable but there's an alternative
cli.js
keep this file es5
#!/usr/bin/env node
require("./run.es5.js");
run.js
// Put the contents of your existing cli.js file here,
// but this time *without* the shebang
// ...
Update your scripts to
"scripts": {
"transpile": "babel run.js > run.es5.js",
"prepublish": "npm run transpile",
}
The idea here is that the cli.js shim doesn't need to be tranpsiled so you can keep your shebang in that file.
cli.js will just load run.es5.js which is the babel-transpiled version of run.js.
You could use another NPM script to add the shebang as the last part of your build process. It ain't pretty but it works.
"scripts": {
"transpile": "babel cli.js --out-file es5.js",
"shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js",
"prepublish": "npm run transpile && npm run shebang",
}
Then your original cli.js would become
import pkg from './package'
console.log(pkg.version);
The resulting es5.js file becomes
'use strict';
var _package = require('./package');
And finally, cli.es5.js becomes
#!/usr/bin/env node
'use strict';
var _package = require('./package');
This could be improved with a clean script.
"scripts": {
"transpile": "babel cli.js --out-file es5.js",
"shebang": "echo -e '#!/usr/bin/env/node\n' $(cat es5.js) > cli.es5.js",
"clean": "rm es5.js cli.es5.js",
"prepublish": "npm run clean && npm run transpile && npm run shebang",
}
Of course, this requires that you are on a system with bash (or some alternate compatible shell), however you could make it cross-platform by rewriting the build scripts to use node implementations of these commands with something like ShellJS.

how to get debugging/verbose information while running package.json scripts?

I have the following scripts in my package.json file
"scripts": {
"start": "watchify -o js/bundle.js -v -d .",
"build": "browserify . | uglifyjs -cm > js/bundle.min.js"
},
when i run npm build, the command executes and moves to a fresh new line in the terminal and the script has'nt been built.
To figure out the problem,
how can i get more verbose/debugging info from running this command?
Have you tried the following command?
npm build --verbose

Categories