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
Related
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.
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.
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.
I updated npm installed node express,even though it is not creating the jason file. can any one please let me know how to fix this.
ramesh#ramesh-PC MINGW32 ~
$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane defaults.
See npm help json for definitive documentation on these fields
and exactly what they do.
Use npm install <pkg> --save afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
name: (ramesh)
ramesh#ramesh-PC MINGW32 ~
First initialize your project. Assuming your project lives in directory "sample" then:
cd sample
npm init // This will ask a bunch of questions. you can mostly just hit "enter". it will create the package.json file for you
npm i express --save
using npm init you can create package.json
To clarify a bit on the previous answers, npm install <package-name> and npm install <package-name> --save may fail if you try to run them from a directory that does not have an npm package.json file.
You might type something like npm install crud --save and get some confusing output like this:
npm install crud
npm WARN saveError ENOENT: no such file or directory, open '/Users/youruser/package.json'
npm WARN enoent ENOENT: no such file or directory, open '/Users/youruser/package.json'
npm WARN youruser No description
npm WARN youruser No repository field.
npm WARN youruser No README data
npm WARN youruser No license field.
+ crud#0.0.28
That's not a very helpful error message -- the level is only "warn", and that last line makes it appear that the requested package got installed somewhere. But if you look in your directory, you will see it remains empty. The explanation of this "riddle" is that some package installers (like PHP's composer) will initialize a project and download the package, others (like Python's pip or npm) do not, so you have to initialize the directory and install packages separately.
Run npm init and answer some basic questions about your project, or copy a viable package.json file that follows the format below:
{
"name": "my-great-node-project",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
}
}
Be careful with the package name! The name cannot match the name of a package you trying to install.
Once the directory has been initialize, you should be able to run your npm install <package-name> --save command and have the package installed into the node_modules directory and have your package.json file updated.
Try not to make any space between words in your project folder name. For instance, instead of "My Portfolio" write it like "MyPortfolio". This worked for me and created json file automatically in my project folder.
I would like to have node modules installed only when you are setting up the test environment. The devDependencies and optionalDependencies will still install if you run npm install <the-module>. Instead, I will store these in testDependencies. To do this it would be nice if there was a *NIX one-liner to get the keys from the JSON object, and pipe that into the npm install command. This can be used for travisci and makes it so the default installation doesn't install these extra libraries.
How do you read the package.json file, extract keys to get module names, and run npm install <keys>? The package.json would look pretty much like this:
{
"name": "the-module",
"dependencies": {
"express": "2.x"
},
"devDependencies": {
"ejs": ">= 0.6.1"
},
"testDependencies": {
"mocha": ">= 0.8.1",
"chai": ">= 0.3.3",
"sinon": ">= 1.3.1"
}
}
The command to run would be this:
npm install mocha chai sinon
Trying to do something like this:
npm install $(read-json ./package.json | extract-keys)
Figured out a horrible hack :p
npm install $(node -e "console.log(Object.keys(JSON.parse(require('fs').readFileSync('./package.json', 'utf-8'))['testDependencies']).join(' '))")
Still looking for a "correct" way.
Check JSON.sh
Also, you can make your one-liner shorter by using require('./package.json') directly - it's the same as JSON.parse(require('fs').readFileSync('./package.json', 'utf-8'))