NPM package for NPM scripts - javascript

I'm trying to write an npm package that will add a specific npm script to whatever package.json on which it is depended. Nothing in the npm package.json / script docs is bringing me remotely close.
I'm basically trying to do this:
I install an npm package (call it 'cool-thing')
cool-thing, by installing, adds an npm script to my existent package.json
I can call cool-thing on the command line and it will perform whatever action is specified in the package.json
Anyone know how I could do this?

I am assuming you are working on a package cool-parent which depends on package cool-thing because you want to run the executable that cool-thing provides.
Normally, you would manually modify the cool-parent package.json to add a script:
"scripts": {
"cool-thing": "cool-thing"
}
Now for user convenience, you would like 1) to automate this modification. Not only that, you want to 2) make the modification automatically after the package is installed as a dependency.
There are some drawbacks to part 2, the developer of cool-parent
might not want to add a script,
might already have a script called cool-thing,
might not have a package.json,
...
I consider making modification outside of the package itself during install time to be an undesirable side effect.
If you still want to do it, you can using a postinstall script in cool-thing. You would need to figure out the location of the package.json of the parent if there is one through working directories and perhaps environment variables that npm provides.

Related

how to change the code of other modules in nodejs

For example.I installed a module in the node_modules. if i change that module, updating node_module will update all modules.What is the usual practice
Look into patch-package
In summary, you install the package and add a postinstall script to your package.json file that calls patch-package:
"scripts": {
"postinstall": "patch-package"
}
Modify the package directly in /node_modules and then call patch-package on the package you just modified:
npx patch-package <package_name>
This will create a local directory called /patches with a diff (or patch) file inside for that package. Commit that folder in with your repo.
Now whenever you run npm install, the postinstall script will also run and apply the /patches that you committed to that package ๐Ÿ™Œ๐Ÿฝ
First, We call it Package instead of Module, Why? because search results on Google are much more accurate when you use that word
Second, to update a specific package use npm update {package name}
Third, you don't change the code of other packages, you can only either install, update, or delete the package. there's this option to override it but it's only possible if the package owner allows overrides.
Update
I decided to update my answer due to the discussion below.
since I have put in mind that you are a beginner and are not entirely sure how to update code or override package code, or maybe even if it's a good idea to change package code, I have given you an unclear answer.
and since the other answer are telling you to use patch-package
then I'm here to tell you that you can fork that package. and install that package from your fork. here's how you do it

Can I include bash/sh files as dependencies in package.json?

I have a bash script which I wrote for publishing modules to npm: publish.sh
As I still work on tweaking this script a lot, every time I change it, I need to make the changes in every copy of it in every npm module I am managing.
Is there a way to include this as a dependency in my package.json file so that I just need to run npm update; npm install in order to update it? Maybe the sh file would need to be executed by some wrapper javascript or something like that..?
"scripts": {
"start": "sh ./scripts/publish.sh",
...
}
If you need access to the script in a single project, you can put it in the "scripts" object in package.json.
"scripts": {
"publish": "/bin/sh publish.sh"
}
The above can be run with npm run publish. See npm run-script docs for more information.
If you literally want to add the shell script as a dependency that can be installed (from the public registry or a private registry) with npm install, you can absolutely do that. Lots of not-JavaScript executable things are available in the npm repository. You'll want to create a package.json for the dependency and specify the location of your shell script with the "bin" entry.
An example package you can look at if you get stuck is notes.sh. The source code is on GitHub. Look at the package.json to see how they specified the scripts to run in the "bin" entry. You'll still need to create a "script" entry in your other project to run the installed shell script (or you can run it as an npm hook) though, unless you're running it manually or spawning a child process in your code or something.

Update package.json in several places after "npm i"

I want to be able to detect any new package installed according to my package.json file, so when ever I do "npm i", it will automatically added to another section on my package.json file.
For example, if I do "npm i axios", It will update in 2 places on my package.json file:
on "dependencies" as usual, and on new section I created: "extDependencies".
Is there any way to detect any new installed packages?
Check this out: npm-scripts documentation
If you want to run a specific script at a specific lifecycle event for
ALL packages, then you can use a hook script.
Place an executable file at node_modules/.hooks/{eventname}, and itโ€™ll
get run for all packages when they are going through that point in the
package lifecycle for any packages installed in that root.
Hook scripts are run exactly the same way as package.json scripts.
That is, they are in a separate child process, with the env described
above.
You could use this to create a postinstall script (bash, python, node.js, etc) that reads the npm_package_name and npm_package_version environment variables and then use those to update the package.json.

Node.js project with no package.json

Is it ok to have a node.js project with no package.json? The ones I see on the internet all come with package.json
What is the effect of having no package.json?
How is package.json created in the first place? Is it created automatically? I am wondering why I do not have package.json
Fundamentally, package.json is a meta file for your application. It lists all the configuration of your application.
What is the effect of having no package.json?
Nothing as far as you're running all your code locally and have no requirement for deployment whatsoever.
Let's setup a scene for you to understand this better.
Imagine that you wrote a brilliant application using node. Now all the chicks in your surrounding want it to play with. It is so fantastic!
Now you want to give it to them and during the development process you `npm install`ed so many things that your project grows beyond 4TB size.
There is no data storage device available to ship that huge code base.
Then the girl of your dream said I want it and I want it now. So you begin searching for app deployment process for node applications.
That is where you stumble upon a magical thing called package.json.
So what you do is you list all your npm installed modules under dependencies property. Then you delete node_modulesfolder, add package.json and commit the entire damn thing in github. Even the .zip file is of 10MB
Then she gets the code.
Types in npm install && npm start (which will install all the dependencies from the package.json` and start your application)
If you have package.json however, that is where you specify all your dependencies.
Using --save flag of npm install
Example.
npm install express --save
How is package.json created in the first place? Is it created automatically?
You can manually create a text file and save it as package.json
OR
A more sophisticated way is to use the command
npm init
I am wondering why I do not have package.json
Me too! :)
You're most probably following a tutorial that doesn't emphasize on initial configuration of the project OR the author of those tutorials presume that the reader has all the fundamentals down to begin with.
It is created automatically if you write npm init.
Then, every package you add using npm install packagename --save will be added to the dependencies list.
You need package.json so that when you want to use your project on another machine you don't have to copy all node_modules, but only your .js files you have written, assets and package.json. You can then run npm install command and it will automatically download and install all the required modules (found in the list of dependencies inside package.json).
You can also manually create or edit it, but it's easier to add --save when installing a module so you don't have to worry about package versions and stuff like that.
Also if you want to create a npm package, an open source project or stuff other people will use, it's either required or the norm to have this package.json file describing your project.
package.json is npm file, if you don't use npm you will not have this file, npm is a great tool if you want to use external libraries in your project but if you don't need it (which is very not likely unless you are doing something very simple), you don't need package.json file too.
To generate package.json file initialize npm in your project using npm init
possible reason thus it exist is you maybe you enter a wrong command like npm i -y, you must initialize the project first, just enter a command npm init -y
Welcome.
Well, if you are running it on your local machine, it's fine. now to answer your last question, package.json is not created automatically.
the npm command npm init -y creates the 'package.json' file. It basically makes sharing your code and installing your codebase easier.

Pointing package.json to a specific React commit installs react-tools (not react)

When I add this line to my package.json:
"react": "git://github.com/facebook/react.git#08e4420019f74b7c93e64f59c443970359102530"
...and then run npm install, I find node_modules/react-tools installed when I expect to see node_modules/react.
What am I doing wrong here?
The code at git://github.com/facebook/react.git is not the same code that gets installed when you npm install react. Instead, the code contains a series of build steps that are used to build the npm package. As far as I know, there is not a way to easily use a specific SHA of the React repo as an npm package; you would need to clone the repo, build the project, and copy it somewhere you can require it.

Categories

Resources