Read package.json version inside package.json - node.js

I have a package.json file that looks like this:
{
"name": "myapp",
"version": "0.0.1",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "webpack-dev-server --port=4200",
"build": "webpack",
"docker-build": "docker build -t myapp .",
"docker-run": "docker run -d --name myapp -p 9090:9090 myapp",
"docker-push": "docker build -t myapp . & docker tag myapp myrepo/myapp:{$VERSION} & docker push myrepo/myapp:{$VERSION}"
},
...
}
Im trying to get the version number when I run npm run docker-push so it will automatically tag the release with the version number of the package.json. How can I achieve this?

You can use the environment variable npm_package_version.
Tip: To see all available environment variables set by npm you can add "printenv | grep npm" as a script:
{
"name": "myapp",
"version": "1.0.0",
"scripts": {
"start": "node app.js",
"version": "echo ${npm_package_version}", // return current version
"vars": "printenv | grep npm" // return all ENV vars with 'npm'
}
// ...
}

Related

electron-rebuild doesn't use correct NODE_MODULE_VERSION

I'm trying to target an electron installation with version 13.4.0 and Node version v14.16.0. However, when I run electron-rebuild on my file it builds for NODE_MODULE_VERSION for 83 (used by Node 14) and not the required 89 (used by electron). Here are some relevant files:
binding.gyp:
{
"targets": [
{
"target_name": "tuxphones",
"sources": ["main.cpp"],
"cflags_cc": [
"-std=c++17"
],
"libraries": [
"-lopus",
"-lpulse"
]
}
]
}
Dockerfile:
FROM node:14-buster-slim
RUN apt-get update
RUN apt-get install -y python3 make libpulse-dev libopus-dev g++
RUN mkdir /build
WORKDIR /build
COPY package.json /build/
RUN npm install
COPY ./native/ /build/
RUN ./node_modules/.bin/electron-rebuild
package.json:
{
"name": "tuxphones",
"version": "1.0.0",
"description": "",
"main": "Tuxphones.plugin.js",
"scripts": {
"rebuild": "electron-rebuild -f",
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"electron": "^13.4.0",
"electron-rebuild": "^3.2.5"
},
"author": "ImTheSquid",
"license": "MIT"
}
How should I fix this so that I can build my module correctly against module version 89?
Turns out the #include statement in my C++ file was including the system's Node library over the specific one for Electron.
I changed this:
#include<node/node.h>
To this:
#include<node.h>

node/npm saying "Missing script <scriptname>" even though it exists in current directory

The package.json is:
{
"scripts": {
"test": "find ./js/tests -name '*.test.js' | xargs mocha -R spec",
"start": "node ./scores.js",
"run": "node ./scores.js"
},
"dependencies": {
"aws-sdk": "^2.734.0",
"body-parser": "^1.19.0",
..
"dotenv": "^8.2.0"
},
"name": "scores",
"version": "1.0.0",
"main": "",
"repository": {
"type": "git",
"url": "something"
}
}
npm install has been done and the package-lock.json created. Why are either npm run or npm run-script working?
$npm run-script ./scores.js
npm ERR! missing script: ./scores.js
npm ERR! A complete log of this run can be found in:
npm ERR! /Users/steve/.npm/_logs/2021-03-01T23_01_56_745Z-debug.log
15:01:56/scores $ls -l scores.js
-rw-r--r-- 1 steve staff 21417 Feb 25 06:24 scores.js
I have identically structured package.json and scripts in other sibling directories that do work e.g. the following works via cd ../keys_server; npm run keys_server
$cat ../keys_server/package.json
{
"scripts": {
"test": "find ./js/tests -name '*.test.js' | xargs mocha -R spec",
"keys_server": "node ./keys_server.js",
"start": "node ./keys_server.js"
},
"dependencies": {
"aws-sdk": "^2.734.0",
"body-parser": "^1.19.0",
"express": "^4.17.1",
..
"dotenv": "^8.2.0"
},
"name": "keys_server",
"version": "1.0.0",
"main": "keys_server.js",
"repository": {
"type": "git",
"url": "something"
},
Update #pkumar noticed that the main entry was left empty. I now updated it to :
"main": "scores.js",
and then tried both
npm run-script ./scores.js and npm run scores and npm run scores.js. However none of them work:
$npm run scores
npm ERR! missing script: scores
The 'main' field is empty in package.json. try adding scores.js there and see if it works
The 'main' field is supposed to be the entry point of the server: https://docs.npmjs.com/cli/v7/configuring-npm/package-json
EDIT: The actual reason it's not working is because "scores" had not been defined under the scripts section. After adding that definition it should work
"scripts": {
..
"scores": "node ./scores.js"
}

npm link is not working, bash: dosmth: command not found

I am trying to do basic CLI with node, but seems like it is not linking from/to correct directory.
I have file named create, which contains command: console.log(process.cwd());
When I run in bash node create, it gives me that outcome:
/Users/katya/Desktop/code/drawer.
However, after running npm link (or sudo npm link), it prints that:
/Users/katya/.npm-packages/bin/dosmth -> /Users/katya/.npm-packages/lib/node_modules/drawer/create
/Users/katya/.npm-packages/lib/node_modules/drawer -> /Users/katya/Desktop/code/drawer
and after that if I run dosmth in bash I get:
bash: dosmth: command not found
I assume there is something to do with ./npm-packages/ appearing in the path.
I tried to delete node completely from computer and install again but did not help.
If you have any idea I would really appreciate your help.
My package.json:
{
"name": "drawer",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin": {
"dosmth": "create"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
Have you tried changing bin to point to your index.js?
{
// Other keys ...
"bin": {
"dosmth": "./index.js"
},
//...
}

How to run forever js with --experimental-modules flag

I have an application which uses esm modules and import statements. It runs fine in development using the --experimental-modules flag with nodemon, but when I run it in production using forever the imports fail because I don't know where to put the flag. When I run npm run build with the package.json as below it says sh: --experimental-modules: command not found.
If I add --experimental-modules to the "script" part of forever-config.json it says the same thing.
I've searched the internet several times. How do use the --experimental-modules flag with forever? I'm running Node v10.16.0.
{
"type": "module",
"name": "application_name",
"version": "1.0.0",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "concurrently \"npm run build:dev\" \"npm run start-server:dev\"",
"build": "npm run build:prod && npm run start-server:prod",
"build:dev": "webpack --config webpack-dev.config.js",
"build:prod": "webpack -p --config webpack-prod.config.js",
"start-server:dev": "NODE_ENV=development nodemon --config nodemon.json --experimental-modules src/server/index.mjs ",
"start-server:prod": "NODE_ENV=production --experimental-modules forever -o ./forever-out.log -e ./forever-error.log start forever-config.json"
},
//rest of package.json left out for brevity
}
forever-config.json
{
"uid": "application_name",
"max": 5,
"spinSleepTime": 1000,
"minUptime": 1000,
"append": true,
"watch": false,
"script": "src/server/index.mjs"
}
I seem to been able to make it work using the command attribute where I pass this flag, e.g:
{
"uid": "myApp",
"max": 100,
"spinSleepTime": 30000,
"append": true,
"watch": false,
"command": "node --experimental-modules",
"script": "./scripts/some-script.mjs",
"path": "D:/MyProject",
"workingDir": "./",
"sourceDir": "./",
"logFile": "./forever-script-log.log",
"outFile": "./forever-script-out.log",
"errFile": "./forever-script-err.log",
"args": ["--someOpt", "someVal"]
}
Note: you can also use args to pass args to your script/app.
In case you have no access to forever-config.json, you can run the command like this:
forever -c "node --experimental-modules" start app.js
You can also set the NODE_OPTIONS environment variable in .bashrc or the like:
export NODE_OPTIONS="--experimental-modules"
(export seems to be required when using NVM)

How to pass params to node scripts from command line?

*package.json**
{
"name": "test-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "node build build_name"
},
"author": "",
"license": "ISC",
"devDependencies": {
}
}
build.js
console.log("Building Code. Output File Name:", process.env.build_name);
Command Line
$ npm run build build_name="web"
I want to pass a parameter build_name from the command line while executing build script. I will use that param inside my build script. Can someone tell me how to achieve this? Also if I did not pass build_name from the command line, can we send a default value from here "build": "node build build_name" instead of build_name.
Use the "yargs" module.
Step 1:
npm install yargs
Step 2:
vi test.js
press i and copy the below code and paste it.
'use strict';
const args = require('yargs').argv;
console.log('Name: ' + args.name);
console.log('Age: ' + args.age);
Step 3 : Execution
node test.js --name=jacob --age=45
Step 4: output
Name: jacob
Age: 45
Let me know if it helps.

Resources