Run npm scripts using local deps - node.js

Currently I run npm scripts using local deps this way:
package.json:
"scripts": {
"test": "node ./node_modules/karma/bin/karma start",
"node-test": "node ./node_modules/jasmine/bin/jasmine",
"build": "node ./node_modules/gulp/bin/gulp build"
},
I don't want to use global deps, since I can forgot to add deps to the package.json. This way when a local dep is missing, then I got an error message and I don't have problems because some deps are not installed globally, e.g. karma plugins.
Is there a better (shorter) way to define npm scripts using the local libs? Is this travis compatible?
edit:
If it wasn't obvious I have the same libs installed globally, but I want to use the local installs by these projects. That means when I start karma with karma start then the globally installed version will start the karma server, which means that if I don't have all of the karma plugins globally installed, then I got error.
Another problem that I have windows, so the solutions described here: How to use package installed locally in node_modules? do not work. Windows does not recognize the #!/bin/sh and the #!/usr/bin/env node head sections and there is no sh command as far as I can tell. At least not in webstorm terminal. Git bash has the sh command, but I want to run these npm scripts from webstorm terminal.
One possible solution could be to fix somehow webstorm so it could use sh from terminal. After that I could use $(npm bin) I assume. But that's just a guess. I am not sure whether this can be done.

npm automatically puts prepends the path ./node_modules/.bin to your PATH env before it executes commands run by using npm run (including the two "magic" shortcuts npm start and npm test)
npm scripts docs
You can just set this up with:
"scripts": {
"test": "karma start",
"node-test": "jasmine",
"build": "gulp build"
}
Assuming that you have karma, jasmine and gulp-cli listed in either your devDependencies or dependencies (so that they're install when doing npm install)
And yes, it is travis-compatible. Here is an example of a package that is tested on travis using tap which is installed locally as a module:
https://github.com/scriptoLLC/couchdown/

Related

npx <package> runs on command line but not in package.json scripts section

I have a package installed in node using
npm i mypackage --save-dev
and it works fine on the command line with
npx mypackage --options
but when I want to have it in the package.json files scripts section it it fails. I tried
"scripts": {
"mystuff": "mypackage --options",
"mystuffnpm": "npm run mypackage --options",
"mystuffnpx": "npx mypackage --options"
}
but nothing works. What is the right way to get it in the scripts section running?
After integrating any package without command try "npx run".It will
install the missing packages in node modules.
Make sure you are running the command in the specific directory.
Try "npm init -y" to make new package.json file and then install the
packages.

can i install a package locally and globally at same time in node.js?

I am working on a project. I have worked with nodemon that is installed globally for development.
I edited my package.json file to add a script to automatically run a nodemon script - as shown below:
"scripts": {
"start": "node ./bin/www",
"dev": "nodemon -e js,pug"
}
Now when another developer runs:
npm start dev
they will surely get a error if they have not installed the nodemon module.
I know that the solution is to install nodemon locally as a development dependency.
Is it possible to work around this problem without installing it locally?
Can I install nodemon both locally and globally at the same time?
simply install it globally and you can use it in any of your project
command :
npm i -g nodemon
now you don't need to install it locally at all to make it work on your project.

package.json scripts that work with npm and yarn?

I am using npm as a build tool and so in my package.json, and some of my scripts depend on other scripts:
{
"test": "npm run lint && mocha"
}
This hardcodes the npm package manager into package.json. How can make this approach to expressing dependencies work with both npm and yarn?
The $npm_execpath environment variable refers to the build tool, so just replace npm with the $npm_execpath:
{
"test": "$npm_execpath run lint && mocha"
}
Both npm test and yarn test will work, and will use the appropriate build tool.
While mjs' answer is great, there's also a small package that is purported to work on all environments including Windows: https://www.npmjs.com/package/yarpm
To use in a project, run yarn add yarpm --dev / npm i -D yarpm and then just use yarpm in your scripts like this:
{
"test": "yarpm run lint && mocha"
}
As the package README notes, you just need to make sure your commands would be suitable for passing through to either yarn or npm: you cannot use arguments/flags that only work on one package manager.

How does "npm" run "npm test"?

I always thought that npm test command just launches what I would write in package.json inside scripts: { test: ...} section. But I have this weird bug when it doesn't work.
So, I have this piece of config in package.json
"scripts": {
"start": "node index.js",
"test": "mocha tests/spec.js"
}
When I try to run tests I type npm test in terminal and had this error:
module.js:340
throw err;
^
Error: Cannot find module 'commander'
But everything is OK when I type just mocha tests/spec.js. Any ideas why is that?
UPDATE:
I've tried to install commander and I had an error Cannot find module 'glob'. After installing glob I have
Error: Cannot find module '../'**
But actually question is why do I have these errors and why is everything OK when running mocha tests/spec.js?
You may have two versions of mocha installed: one globally (npm install -g mocha) and one locally, which appears to be broken.
When you run a script through npm, either as npm run-script <name> or with a defined shortcut like npm test or npm start, your current package directory's bin directory is placed at the front of your path. For your package that's probably ./node_modules/.bin/, which contains a link to your package's mocha executable script.
You can probably fix this by removing the local mocha and reinstalling it with --save-dev:
rm -rf node_modules/mocha
npm install --save-dev mocha
That should get you a working local copy of mocha with all its dependencies (commander etc.) installed.

npm script running for preinstall, but not for preupdate

I have a script referenced in packages.json for a node app.
The script is run fine when I do
npm install
but not for
npm update
The excerpt from packages.json is
"scripts": {
"start": "node app.js",
"preinstall": "node scripts/install.js",
"preupdate": "node scripts/install.js"
}
The whole file is at https://github.com/Pike/outreach/blob/master/package.json.
As I understand it they've disabled the scripts for npm update (preupdate/postupdate) -- something about best practice...
It's a complete pain in the ass -- they want you to use node-gyp and .gyp files for building node modules -- it has a dependency on python! No thanks!
I'm still banging my head on the keyboard over this -- since we cant install python on our production servers.
Update
Python is available on most Linux Distros, not too much of a big deal

Resources