npm pack with modified package.json content - npm

I would like to create a npm package using npm pack or yarn pack with a customize package.json in the package.
In other terms, I would like to suppress devDependencies, change scripts from the initial package.json to the package.json included in the tgz tarball.
Is ther a simple way to do that ?
Thanks,
Pisamad

Related

package-lock.json in npm workspaces

Given an npm workspace with the following structure
workspace
package.json
packages
package-a
package.json
package-b
package.json
When I run an install command in package-a this will generate a package-lock.json file in the root of the workspace but not in the package.json file itself.
Is there a way to also generate it in the packages?
I don't know if this solves your problem, but you can specifie the folder in which you would install with --prefix
npm install --prefix ./install/here
you can use the lerna tool to manage your workspace and install dependencies in each package. you can generate package-lock.json files in each package in your workspace.
The Original Tool for JavaScript Monorepos. Monorepo means a repository with multiple packages.
lerna.js.org
I hope this answer will show you the right direction.
In most cases, running npm install within that package directory should do the job. But as you said that this is creating a global package-lock.json. This might be because the package you are installing might be specifying the global path using the prefix field.
The "prefix" field, specifies the location where the package's dependencies should be installed.
So one thing you can do is to go to the package.json in package-a and then either remove the prefix field from the package.json file OR set its value as following :
{
"name": "my-package",
"version": "1.0.0",
"prefix": "./",
"dependencies": {
...
}
}
Now when you run npm install it should install the packages locally and make a local 'package-lock.json`.

Package.json pasting a package name in bad?

What happens differently when you go into your package.json and paste a package name in and do npm i vs. doing it the real npm i package-name?
package.json:
"dep": 1.0.0
vs
npm i dep --save
We have a build error and learned can bypass it by pasting. I know it isn't kosher but I really want to know why and what consequences that causes?
npm install dep doesn't add the dependency to the package.json file.
You have to add --save or --save-dev to add it to the package.json file.
Besides that, npm install will always serve you the latest build (in most cases the version tagged as latest (see npm docs)), unless you specify a specific version.
If you want your lock file to update, you have to delete the file before running npm install to generate a lock file with the dependency included (for more info check out this GitHub issue)
In conclussion it shouldn't make much of a difference if you manually add the dependency to package.json file and install it with npm install, unless the latest version of your dependency is broken.

How to update package.json dependencies when linking from globally installed packages?

I organize my development projects installing globally all the npm packages I need with:
npm -g install [package]
Then I simlink individually the dependencies I need for each project with:
npm link [package]
This way, I have to update manually each package.json file to add the dependency, and when I upgrade the global node_modules I have to go and update all the package.json projects.
For this first issue I tried npm link [package] --save but it doesn't add the dependency to package.json and if I use npm install [package] --save it installs the package locally, thing I don't want.
Is there any way to be able to not have to configure package.json manually and be able to have an updated configuration of package.json from many different projects in a easier way?
Yes you can install npm-check-updates, you can find the install and guide here:
https://www.npmjs.com/package/npm-check-updates
when running 'ncu' on the command-line in your root-folder where your package.json is, it will list the packages that can be updated and by running 'ncu -u' on the command-line it updates all the packages for you.

How to create tgz file with version using npm pack?

I'm trying to create npm package (tgz) without publishing however it's not clear how to specify the version of the module.
When running npm pack it creates filename with version 0.0.0, does anyone has example to share?
npm pack reads the version from your package.json file. You can set the version with the npm version command:
npm version 1.2.3
If you don't yet have a package.json file, create it with npm init.
above answer is valid,
just adding some code and a small tip:
{
"name": "my-cool-package",
"version": "1.0.0",
(...rest of you package.json details)
}
npm pack command in any terminal will give you my-cool-package-1.0.0.tgz.
This will have all contents from you package.
Extra info:
In case you want to ignore any file eg node_modules add those into .npmignore in the same folder and they will be ignored.

how to add jquery-slider to the dependency list in browserify?

Description : I have package.json and bower.json for handling dependencies in my current schema. In which file should I add the "jquery.slider (version 1.1.0)" and how to do it?.
Currently,I have added the dependency in bower.json like below
"dependencies": {
"jquery.slider": "^1.1.0"
}
I have also tried it added in package.json and also in both ,but the slider is not even getting displayed.
Can anybody help me on this?
You could install it with npm, so it would be added to your package.json automatically.
npm install --save jquery.slider
Note the --save option within the command. This will add the module to the dependencies.
Then, if someone will clone the project he can simply run
npm install
to install all dependencies defined in package.json, included jquery.slider, automatically.