npm install + extra command - javascript

Is it possible to append to a task to "npm install". I have a node project in which I'm using Grunt as task runner. To fully install the project, one of course needs to get the dependencies by running "npm install". After this it is still not fully setup however, and one needs to also run a command via grunt. Now I'd like all required installation tasks to be done with a single command.
Is it possible to append a task to run to "npm install", so it becomes "npm install ; grunt mytask" when someone runs "npm install"?

Your package.json file can contain a "scripts" property: https://npmjs.org/doc/misc/npm-scripts.html
The "postinstall" script sounds like it would fit your needs.

I re-read your post, and I think I understood it wrong the first time.
I believe what you are looking for is here: https://npmjs.org/doc/scripts.html
Hth,
Aaron

Related

How does npm know to start my React app when I type `npm start`? [duplicate]

When I type "npm run test" in the command line, npm goes to package.json, to the "scripts" section and tries to match "test" there.
So far so good.
Now, the line behind "test" is the following: "JASMINE_CONFIG_PATH=./spec/support/jasmine.json jasmine-run" but the first part (everything except "jasmine-run") can be removed witout problems. [I have a similarily structured project where it works, so I can test these modifications]
now:
WHERE is npm looking for "jasmine-run" ???
Because since I have a project where the script provided work, I could look for it, but the answer is: in the node_modules folder next to package.json is a module, in whose package.json has, in the "bin" section:
"jasmine-run": "tools/jasmine-run/jasmine-run.js",
However, this exact setup exists in both projects. and in one everything works, while in the other "jasmine-run" cannot be found.
As ana lternative to an answer I'd also take a proper explanation (or source) on how/where npm run actually looks for its stuff, because then I could probably find the error myself.
When you run a script with npm like :
npm run-script <name>
or with a shortcut like
npm test or npm start,
your current package directory's bin directory is placed at the front of your path.
For your and in many of the cases will probably be
./node_modules/.bin/,
which contains a link to your package's executable scripts.
Anyway you have all the explanation how npm runs work here : https://docs.npmjs.com/cli/run-script
npm run adds node_modules/.bin to the PATH provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin prefix.
Check if you can locate jasmine-run inside node_modules/.bin directory.
For reference have a look at this post: https://docs.npmjs.com/cli/run-script
In case anyone else ends up here looking for the answer after running create-react-app, here is where I found them.
The line from package.json is
"start": "react-scripts start"
In VSCode, open the node_modules and scroll down to react-scripts.
Then open up 'scripts', and there they are.

Good way to switch between 2 versions of the same dependency in package.json?

Turns out you can't have comments in JSON files, and it's a bit awkward to have people refer to some documentation telling them what line to copy/paste in and where in order to achieve this.
I think I can make a python script to copy/paste in one of two package.json files depending on what flags they pass in, but that feels overcomplicated.
I think I can include both dependencies (under different names) but that would create a requirement for both to be available, which is not good either.
Looking for ideas/thoughts on a good way to accomplish this. I have a release and dev version of the same dependency and I often need to swap between the two. Would like to improve the workflow beyond just having a notepad on the side with the two lines pasted in it...
yarn and npm already do this job, why not use them?
Releases
Tag the dev versions when you release them
yarn publish --tag dev dep
npm publish --tag dev dep
Then reference the tag at install time yarn install dep#dev.
Local
For local dependencies, npm and yarn provide the "link" command.
In your dependency dir run yarn link
In you project dir run yarn link dep
You could document the commands as scripts so easy to run or view.
"scripts" : {
"dep:local": "yarn link dep",
"dep:dev": "yarn install dep#dev",
"dep:latest": "yarn install dep#latest"
}

Jest: `npm test` not displaying "Watch Usage" options in Windows Git Bash

I am new to React and attempting to use npm test.
From the docs, it is my understanding that every time npm test runs, it starts the watcher. The watcher is supposed to display a list of commands titled "Watch Usage". This includes commands such as a, f and q.
When I run npm test, watcher appears to work because it runs the tests and reruns them if I save a file.
However, it does not display the "Watch Usage" list and if I type any of the "Watch Usage" commands, nothing happens.
How can I enable the "Watch Usage" commands?
I was attempting to run this command in Git Bash. I have not found a solution to make it work in Git Bash.
However, you can instead run the command in cmd.exe and it will work properly.
The Jest VSCode extension can also be helpful.
There is a problem with running Jest inside Git Bash on Windows. The terminal codes for updating the screen are not correctly handled, and I've been unable to find a reason or fix for this searching Google.
If you run npm test from a DOS prompt, then it seems to work fine, but if you run npm test from a Git Bash prompt the progress feedback from Jest is missing.
You can enable colors by running npm run test -- --colors which gets Jest to output the color status for tests in Git Bash, but strangely the colors are working but the progress is still not shown.
The only successful work around that I've found is running Jest from inside winpty if you're using Git Bash.
winpty npm.cmd test
So what I do is add an alternative npm script to my package.json file for running on Git Bash inside Windows.
"wintest": "winpty npm.cmd run test",
Then you can just run that instead.
npm run wintest
It's not a perfect solution, but at least you can see the progress correctly.
You can activate watch mode, modify your package.json:
"scripts": {
"test": "jest --watch",
}
run npm test

After npm reads package.json, what runs Electron?

I'm just starting to learn about how JavaScript, HTML, and Electron all work, and I want to know what runs electron . in the "scripts" -> "start" of package.json, because I can't tell what does and that kind of wizardry makes me nervous.
According to the man pages for npm, what npm start does is that it reads the package.json, looks at the script under "scripts" -> "start" -> some_script, and then runs some_script. Sometimes, some_script is something like node foobar.js, which makes sense to me, since I can run that from the command line. NodeJS is executing foobar.js. However, in the case of the electron-api-demos, some_script is electron .
You can download and run electron-api-demos via
git clone https://github.com/electron/electron-api-demos
cd electron-api-demos/
npm install && npm start
In order to try to figure out what is running electron ., I've run it in the node shell, and I've tried running node main.js. I've even tried opening up the node shell and running
electron-api-demos#2.0.2 start $DIR/electron-api-demos
electron .
(which is exactly the output of npm start). None of them worked, because none of them started up the Electron application. At this point I'm very puzzled at how, exactly, the start script gets executed at all.
So I guess my question is: does there exist a command (that I can use on the command line) to start up this Electron application, without using npm? If not, what is npm calling to start up this Electron app?
I apologize if this question has been asked before, but I all the sources I found didn't seem to go into any further detail about what, exactly, is done when npm start is run and how it executes electron . . Thank you for your time!
Command line interfaces installed with npm are put in the node_modules/.bin/ directory. You can't just run them from the command line because that directory isn't in your PATH (unless you put it there, or you installed it globally).
So, if you want to run electron without npm start, you can run ./node_modules/.bin/electron .. Since this is a bit verbose, newer versions of npm provide the command npx to run things without the ./node_modules/.bin/ part, so npx electron . also works.
Since npm scripts often use the packages you've installed, they automatically add node_modules/.bin/ to the PATH before running your command. As a result, the start script can just reference electron directly.
npx can do some other cool things too – npm has a blog post about it.
When you run npm start , it by default run command corresponding "start" key of script property of package.json like
"script":{
"start": "ng serve",
"launch":"electron main.js" or "electron ." // main.js located in the same dir
"test": " ng test"
}
same when you run npm run launch it will trigger the command corresponding of the "launch" key of script property of package.json file. like run electron main.js command and your application will launched.
so if you want to run the your electron application directly like electron main.js then install the electron module globally using command npm install electron -g then simply run the electron main.js command and your application will start.

Reverting to Previous Version of Package.json When a Dependency Bug Arises

While in the process of updating an Angular app I and colleagues are working on, I ended up running "npm update" when I meant to run "npm install". Doing so led me on a bit of a rabbit trail because of course now all my dependencies - AND their dependencies got updated in the process. From there I had to resolve certain conflicts to get the new versions to work correctly. However, this also led me to a point where a bug in one of those dependencies is preventing my app from booting up. According to the the Angular github repo, the issue is being worked on.
My question is, how can I revert to my previous setup in the meantime? I tried copy and pasting the package.json file as it originally existed before my "npm update", deleting my "node modules" folder, and running "npm install" again. But this doesn't resolve the issue. Is there a way I can be assured of reverting to my previous working setup?
The process you described should work:
Get an old copy of your package.json from your repository at the state you know it worked
Run rm -rf node_modules to remove the node_modules folder
Run npm install to install again
If that didn't work, verify that you:
are in the correct directory (that should contain package.json and node_modules)
have permissions to clean the node_modules folder (chmod 777 node_modules)
the package.json that is written in the file system is actually the restored one (sometimes an IDE or Git can create a weird shadow copy where you think it's one way, but it's really another). You can tell this by using cat package.json and inspecting the output

Categories