How does "npm" run "npm test"? - node.js

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.

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.

Run npm scripts using local deps

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/

mocha running with NPM test but not regular mocha CLI command

I am trying to understand what I am doing wrong in this instance. I have a Node.js project with the following in my package.json
"scripts": {
"test": "mocha --recursive ./src/setup/*.js ./test/**/*.js"
},
"dependencies": {
"mocha": "^2.2.5"
}
When I run 'npm test' the mocha tests are run correctly:
$ npm test (successful run)
However when I try to just run the mocha command I have there in my package.json
$ mocha --recursive ./src/setup/*.js ./test/**/*.js"
This errors with:
-sh: mocha: command not found
I do not have mocha globally installed, I only have installed it via npm to this specific project.
If I install mocha globally then it works. Why doesn't it work when I have simply have mocha installed in the current directory's node_modules, yet it does with 'npm test'?
npm scripts automatically add mocha to the PATH:
If you depend on modules that define executable scripts, like test suites, then those executables will be added to the PATH for executing the scripts.
https://docs.npmjs.com/misc/scripts#path

Elastic Beanstalk dependencies not available in npm install?

I have read EB Documentation to understand how i can set external dependencies for my app. My app is to be installed in a nodejs container which automatically executes npm install and npm start. So to build the static dependencies I use
"scripts": {
"postinstall": "bower install -F && gulp build",
"start": "node server.js",
"test": "NODE_ENV=test mocha server/test --recursive"
}
Which isnt working fine due to error in gulp build, which is as follows
[12:03:22] Starting 'styles'...
{ [Error: spawn ENOENT: Missing the Sass executable. Please install and make available on your PATH.]
message: 'spawn ENOENT: Missing the Sass executable. Please install and make available on your PATH.',
showStack: false,
showProperties: true,
plugin: 'gulp-ruby-sass',
__safety: { toString: [Function] } }
[12:03:22] 'css' all files 0 B
So I did this
packages:
rubygems:
sass: ''
and placed the file in .ebextensions/01config.config but no change in error. Have tried doing numerous other things but ended up doing the following as my last idea
"postinstall": "bower install -F && gulp build && sh postgulp.sh",
and creating a postgulp.sh file in the project root
#!/bin/bash
pwd
ls
if hash sass 2>/dev/null; then
sass --update ./client/css:./public-debug/css --sourcemap=auto;
else
echo >&2 "I require sass but it's not installed. Continuing without sass.";
fi
Still no luck. Even though I have sass in rubygems dependency but its not in the PATH. Why? When are ebextensions scripts executed as opposed to npm install and postinstall? Why is some thing that is installed in ebextensions only available to those scripts and not to npm? Why dosent gulp-ruby-sass pick up sass?
There can be two versions of ruby on the instance - one in /usr/bin and another one in /opt/elasticbeanstalk/lib/ruby.
Since you said SAAS is installed in the latter you need to make sure that the version of ruby on path when running your app is the same.
Most likely node is trying to use system ruby in /usr/bin and hence cannot find the gem.

Resources