Node package.json "bin" value, command not working - json

I used nodejs on my vserver to make a tiny script to manage users in a db.
In package.json I added the "bin" and set it to my script. My attempt was to make a command available on the whole server so I dont need to go to the directory where the script lies and write "node usermanager.js".
I used npm link and it seemed to work fine:
/home/sl4yer/bin/cl9wnhook -> /home/sl4yer/lib/node_modules/cl9wnhook_usermanager/usermanager.js
/home/sl4yer/lib/node_modules/cl9wnhook_usermanager -> /home/sl4yer/cl9wnHook/usermanager
package.json btw is:
{
"name": "cl9wnhook_usermanager",
"version": "1.0.0",
"description": "User manager for cl9wnHook",
"main": "usermanager.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"cl9wnhook": "./usermanager.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"commander": "^2.9.0",
"js-sha512": "^0.2.2",
"readline-sync": "^1.4.5"
}
}
so using the command "cl9wnhook" should work.
But when I call it, I get:
[sl4yer#lynx usermanager]$ cl9wnhook
: No such file or directory
Any idea?

sudo npm link
I did it, and successfully.

Try adding
#!/usr/bin/env node
on the top of your usermanager.js file. It should work.

Pack the package
npm pack
Then install it globally to run it from any folder.
npm install --global <package_file>.tgz

your bin in your package.json like this:
{
"bin": {
"app": "bin/app"
},
}
but first in your app file you should add at the top this #!/usr/bin/env node
Note
This will help you

Really things can mess up from time to time when working with NPM or Yarn. In these cases first do a complete clean install.
rm -rf ./node_modules
yarn install
npm install
If that doesn't solve your problem then check the bin property in package.json, if it is mapping to the cli file correctly?
You can check what is packed by running yarn pack.

Related

what would be the right way to run and express generated app with pm2 on linux

how would be the right way to run an express-generator created with pm2 ?
which would be the right adjustments i should make to run with pm2?
As it says in his documentation I should run the app like :
DEBUG=myapp:* npm start
Thats ok when i run with npm but what should I change to run it with pm2?
in the package.js file lays..
"scripts": {
"start": "node ./bin/www",
"test": "mocha --timeout 10000"
},
I have changed this to like, but is not added in pm2 monit:
"scripts": {
"start": "pm2 start ./bin/www",
"test": "mocha --timeout 10000"
},
Ok I have done like this:
In terminal run the app like
pm2 start ./bin/www
where resides de app.
that way it will run a your app as a "www" and you can monitor with pm2 monit.

Watch and reaload for polymer serve / polyserve?

I'm trying to figure out a way to make polyserve / polymer-cli to reload my browser each time a "watched" file changes but I haven't found anything apart of adding Livereload to the HTMLs files that I think is going to be a mess due that when developing web components I load separate HTML files.
For auto reloading using Polymer CLI as a server in your project you should have node / yarn installed. Then you need to install browser-sync locally.
npm install -D browser-sync
or
yarn add browser-sync --dev
Your package.json file will need to look something like:
{
"name": "MY-ELEMENT",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "npm run serve | npm run watch",
"serve": "polymer serve --port 8080",
"test": "polymer test",
"watch": "browser-sync start --proxy localhost:8080 --open --startPath \"components/MY-ELEMENT\" --files \"**/*.html\""
},
"devDependencies": {
"browser-sync": "^2.18.13"
}
}
(If its a regular Polymer app you should remove the --startPath argument as that is for Polymer component development)
I'm using polyserve-watch which watches, serves and reloads webcomponents using polyserve and browser-sync``.
Best regards

How to Automate version number in package.json with Jenkins

Here is my package.json looks like:
{
"name": "Myproject",
"version": "0.4.13",
Note:Here 4 is not the minor version.0013 is minor
"dependencies": {
"lodash": "^4.0.0",
"vinyl-fs": "2.2.1"
},
"repository": {},
"devDependencies": {
.........
......
How can I automate versioning of package.json using Jenkins build.
Required format should be:
0.4.13-$BUILD_NUMBER
So far I try to do it using sed command:
sed -i "s/version: .*/version: 0.4.13-$BUILD_NUMBER/" package.json
But it's not updating version number in package.json file.
Also used
npm version 0.4.13-$BUILD_NUMBER
FYI:The generated build artifact should look like 0.0013-1.war
If you're using grunt, you could use the recommendation here.
Alternatively, there's a built in function in npm that does this for you. Run npm version, docs here.

How do I create a package.json file?

In Mac Terminal:
package.json This is most likely not a problem with npm itself.
npm ERR! package.json npm can't find a package.json file in your current directory.
Please include the following file with any support request:
npm ERR! /Users/stickupartist/portfolio/npm-debug.log
stickup-artists-macbook-pro:portfolio stickupartist$ npm init
This utility will walk you through creating a package.json file.
What utility is being referred to?
And next:
Use `npm install <pkg> --save` afterwards to install a package
and
save it as a dependency in the package.json file.
Name: (portfolio)
I type:
npm install <portfolio> --save
And the terminal prints out:
Sorry, name can only contain URL-friendly characters.
What am I doing wrong with my naming? I'm working on my local machine with Meteor, on Mac OS X.
To create the package.json file, you can run npm init (and go through its options) or manually create the file based on these rules.
Here's a simple package.json file:
{
"name": "my-cool-app",
"version": "1.0.0",
"description": "This is my cool app",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
},
"author": "Me",
"license": "MIT",
"dependencies": {
"jquery": "1.1.1",
}
}
Now, as far as the error:
Sorry, name can only contain URL-friendly characters.
It means that the package name doesn't meet one of the naming rules, mainly:
package name must not contain any non-url-safe characters (since name ends up being part of a URL)
This is most likely happening because you wrapped your package name in <>.
<> means it is a placeholder for a value. When actually typing it in, you should overwrite it (and anything it wraps) with some appropriate value, in this case a valid package name.
It is how you would define an npm install command, not use it:
Definition:
npm install <package_name_goes_here>
Usage
npm install portfolio
Use: npm init -y
Then install your packages.
That worked for me when I had the same problem.
See nem035's answer to create package.json (just npm init).
For your other problem: in npm install <pkg> --save refers to the name of a package. You can install the package with its name, without brackets. For example, npm install portfolio --save
Log out of the session. Then re-login and try npm install -y. This has worked for me.

Edit package.json from command line

I'm trying to add or edit a variable in my package.json from a shell script. So if i have a package.json like this:
{
"name": "my-project",
"description": "Project by #DerZyklop",
"version": "0.0.0",
...
I want a command like
npm config set foo bar
that adds a new field like
{
"name": "my-project",
"description": "Project by #DerZyklop",
"foo": "bar",
"version": "0.0.0",
...
...but unfortunately npm config set just edits the ~/.npmrc and not my package.json.
The package.json is just a json file, so you could use the tool json. To install it use:
npm install -g json
Then you can edit a file in-place. More information here.
Example
$ cat package.json
{
"name": "my-project",
"description": "Project by #DerZyklop",
"version": "0.0.0"
}
$ json -I -f package.json -e "this.foo=\"bar\""
json: updated "package.json" in-place
$ cat package.json
{
"name": "my-project",
"description": "Project by #DerZyklop",
"version": "0.0.0",
"foo": "bar"
}
You do have a native NPM command:
npm pkg set 'scripts.test'='jest'
Which is really helpful when you want to share a command. Instead of asking someone to install some cli tool, you can simply share this.
BTW, it's even more helpful when you use NPM workspaces, in which case you can change all the packages together:
npm pkg set 'scripts.test'='jest' -ws
If you don't want to install anything, you can also use a one-line script to modify the package.json:
node -e "let pkg=require('./package.json'); pkg.homepage='${CI_PAGES_URL}'; require('fs').writeFileSync('package.json', JSON.stringify(pkg, null, 2));"
If you don't want to install sponge or json, you can also do
echo "`jq '.foo="bar"' package.json`" > package.json
You can also use jq and sponge (moreutils package) like this :
jq '.foo="bar"' package.json | sponge package.json
With an environment variable :
jq --arg h "$HOMEPAGE" '.homepage=$h' package.json | sponge package.json
I wanted to update only the version property in package.json and this is what worked for me:
# this sets the version
# in package.json to 1.0.2
npm version 1.0.2 # creates a git-tag too
npm version 1.0.2 --no-git-tag-version # only changes the version without creating a git-tag
There's also a npm package for doing this called npe: https://github.com/zeke/npe
cd some/node/project
# Get stuff from package.json
npe name
npe scripts
npe scripts.test
npe repository.url
open $(npe repository.url)
# Set stuff in package.json
npe name foo
npe scripts.start "node index.js"
# Keywords string will be turned into an array
# If commas are present, they'll be the delimiter. Otherwise spaces.
npe keywords "foo, bar, cheese whiz"
npe keywords "foo bar baz"
# The current working directory's package.json is used by default,
# but you can point to another package file with a flag:
npe name --package=some/other/package.json
npe name other --package=some/other/package.json