Nodeunit with grunt can't find module 'tap' - node.js

So, armed with this tutorial, I decided to add some unit testing to my node.js app. My gruntfile seems to be OK, and when I type grunt nodeunit my only test runs just fine, but after that, it crashes with error Fatal error: Cannot find module 'tap':
$> grunt nodeunit
Running "nodeunit:all" (nodeunit) task
Testing db.test.js
.OK
Fatal error: Cannot find module 'tap'
I didn't know anything about this module, but after googliing it up, it seemed that it's something nodeunit would require. And indeed, there it is: $MY_NODE_APP/node_modules/nodeunit/node_modules/tap exists, and when I launch node in $MY_NODE_APP/node_modules/nodeunit and type require('tap') in interactive console, I get some object with a lot of stuff that gives me an impression that it works as it should.
So, the obvious question: why do I get this error, and how can I fix it?
Gruntfile:
module.exports = function(grunt) {
grunt.initConfig({
nodeunit: {
all: ['./**/*.test.js']
}
});
grunt.loadNpmTasks('grunt-contrib-nodeunit');
};
Update: Installed tap to my project, but it didn't help too.

I landed on this question and have since resolved my own issue. I'll post my resolution as it might be helpful to someone else:
First the similarities:
I was just getting my Gulp scripts setup, and I got this error when running my tests:
Fatal error: Cannot find module 'tap'
Which didn't make sense because I wasn't using that library!
How I resolved:
Turns out my paths were all kind of screwed up in my gulpfile.js. Therefore I was trying to unit test all of the modules in my node_modules folder!
Here's the new path:
paths: {
mocha: [
'**/*.test.js',
'!node_modules/**/*.js'
]
}

The issue for me was that I was inadvertently running the tests within the node_modules directory. My npm test was mocha **/*.test.js and when I changed it to mocha src/**/*.test.js all of a sudden the message went away. Makes sense really.

I have solved the problem by adding another '*' right after "mocha" it is related to the folder's path so it may also fix the problem in other cases.

I solved the issue (well, this issue — it wasn't the last one) by installing the tap module manually.

Related

Angular build - Module not found: Error: Can't resolve 'console'

I have a problem with my Angular project build, and ultimately deployment to heroku. I'm using an old(ish) npm package called binary parser, which causes the following error on when I build / deploy to heroku:
Module not found: Error: Can't resolve 'console' in '/tmp/build_e75b87f248f44978f9537d83b3172254/node_modules/binary-parser/dist'
The binary-parser.js has a line require("console"); which is used in exactly one place, so local builds succeed and the application works perfectly, if only I remove console from that line altogether. But as, heroku installs node modules when deploying, this only helps when I manually build the prod version.
I have installed typings for binary-parser and for TS, and also included "types": ["node"] in both tsconfig.json and tsconfig.app.json compilerOptions.
As angular these days doesn't really allow for webpack configuration, I've tried adding global.console = global.console || require('console-browserify');
(or)
global.console = global.console || require('console');
to my polyfills, to no avail.
Any ideas on how to solve this? Do I need to configure a custom webpack to circumvent this? I'll gladly post additional information if necessary!
Here's a possible cause, may or may not be what you or others reading this question are experiencing...
My IDE's auto importing added import * as console from "console"; when I typed console.log.
Solution was of course to remove that import statement.
After trying for multiple hours to come up with the right configuration, the only solution I could come up with was forking the repo in question and changing tsconfig target from es5 to es6, which got rid of the console import altogether upon compilation.

Error: Cannot resolve module 'apply' webpack angular2

I am not really sure how I got into this pickle.. I uninstalled my global webpack and reinstalled it locally to dockerize my project. Then I got a trail of errors, resolved the ones I could and I'm left with this
http://puu.sh/orazd/b8a85ac5c6.png
I don't use any module called apply, most certainly not in every file
I had tried resolveLoader previously
and it had worked for me.
My configuration is as follows:
resolveLoader: {
modulesDirectories: [
'/users/path/a/node_modules'
]
},
Hope this helps.
Though not sure, I think maybe something wrong, like conflicts, in the typings folder. Would you try to remove typings folder and run npm install again.
The answer turned out to be
silly enough.. the error was a little misleading but.

Grunt only runs default task

Using OSX Maveriks, iTerminal, zsh.
Problem: Grunt runs only the default task regardless of the task you specify.
Problem: It only happens in 1 computer.
What can be the reason for grunt not to run but the default task. I've been working with Grunt for a long time now and we use it at the company I work at. Everything has been working perfectly smooth except for 1 single coworker that when he runs grunt myTask is like if he were running just grunt, even if I run a made up task like grunt myNotExistentTask it ignores it and again looks like it runs grunt.
I did check for stupid errors:
Checked that I'm accessing the same project folder (multiple times).
I deleted the default task to see if it complained and it does.
I checked that the Gruntfile was not throwing errors. Then I forced to throw errors to see if it would complain, and it does.
Runned npm install.
Check both bashrc and zshrc for a grunt command or something.
Obviously grunt is accessible.
Runned it in the default terminal and in iTerm.
"Have you try to turn it on and off?" Yes I did restar the laptop.
I tried with multiple coworkers laptops and it only happens in the one laptop.
I did not have time to (but I will check for these):
test creating a simple Gruntfile somewhere else in his laptop, but I have a weird feeling that it would behave the same way.
reinstall grunt. Would that help?
grunt --version or others for that matter, I assumed that it has the specified version in the package.json.
we are using:
"grunt-cli": "^0.1.13",
"grunt": "^0.4.4"
The code is not causing the issue here because every computer runs it but one. I can't show the actual code but I can give you the boilerplate just in case there is some mysterious thing I don't know about:
module.exports = function(grunt) {
grunt.initConfig({
pkg : grunt.file.readJSON("package.json"),
someTask : {
options : {
},
target1 : {
}
}
});
grunt.loadNpmTasks("dependency1");
grunt.loadNpmTasks("dependency2");
grunt.loadNpmTasks("etc");
grunt.registerTask("default", function () {
// creates a server with one of the dependencies
});
grunt.registerTask("w", function () {
// creates a watch with one of the dependencies
});
};
There is a reason behind why we are not simply doing something like grunt.registerTask("default", [".."]). Basically, our projects are too big and we need to change settings for each task to increase performance (e.g. not to watch too many files, etc).
I googled this different ways and couldn't find anything about this happening before. Any ideas are welcomed.
Edit
I forgot to mention that he has been working with the project for a while, we just recently noticed because he never needed to use any other task but the default task until a few days ago when he needed to use a different task, in summary, we don't know when or why it started.
After:
Deleting and cloning the repo.
Deleting npm_modules.
Reinstalling everything.
Creating an isolated project to test this issue.
and failing, we checked for the paths to grunt thanks to a comment by #XavierPriour
Using zsh with iTerm:
➜ folder git:(branch) which grunt
/usr/local/bin/grunt
➜ folder git:(branch) type -a grunt
grunt is /usr/local/bin/grunt
grunt is /usr/bin/grunt
We deleted the extra grunt binary:
➜ folder git:(branch) cd /usr/bin
➜ bin sudo -rm grunt
And everything started working correctly. I assume the issue is he installed grunt globally instead of just grunt-cli, although I'm not sure.
The most popular answer to this question shows the dos and don'ts of how to install grunt.

Issues installing swiper.js (node.js/grunt noob)

Having issues getting swiper.js to work. Requires grunt/bower. I'm completely new to js generators and am only able to get through about half the walkthrough before running into issues.
When I type $grunt dist into terminal, I get the following response: -bash: dist: command not found
What step am I missing?
Followed the idangerous steps to the letter: http://www.idangero.us/sliders/swiper/plugins/scrollbar.php
I have most often see "X command not found" for one of the following reasons:
The project's Grunt dependencies aren't installed or aren't installed properly
Your Gruntfile is mis-configured or missing
To fix this, first make sure there is a package.json file in your project. This will tell the npm what dependencies the project has and install them accordingly (assuming the package.json file is also configured correctly).
Next, make sure you have installed grunt correctly.
If you're still having issues, open your Gruntfile and search for:
grunt.registerTask('dist
This will show where the "dist" task is being defined. If you for some reason don't find it, then there's your problem. If you do find it, then check the proceeding commands inside square brackets that look like this:
grunt.registerTask('dist', ['clean', 'dist-css', 'copy:fonts', 'dist-js', 'dist-docs']);
'clean', 'dist-css', etc. are all other tasks defined in the Gruntfile, and there could be an issue with those as well.
If there's an issue with your package.json or Gruntfile, then trying re-installing the project with bower and repeat the above steps to ensure that it's not an issue on your end.
If it's not, then something is probably wrong with the author's source code.

Exporting the same object that is required confuses mocha

I'm working on a node app that uses mocha to run unit tests.
When I run this command:
mocha --compilers coffee:coffee-script --reporter spec ./test/unit/*-test.coffee
I get this error:
ERROR: Unknown option --compilers
It seems mocha is confused, because it definitely has a compilers option. This error started happening when I added a new file to the project. It's the only output I can get mocha to generate. --debug does nothing.
Let's say I have a package called person installed. I want to configure this package globally so that I can import the configured object anywhere in my project. To do that, I import person, configure it as a driver, and then export it again.
However, when I import it (either in Car.coffee or Car-test.coffee), mocha fails with the above error.
Driver.coffee
driver = require 'person'
driver.setSkill "Drive"
module.exports = driver
Car.coffee
driver = require './driver'
...
Car-test.coffee
driver = require '../../src/driver'
...
Note that this works fine if I'm just compiling with coffee and running the node project. There's no issue importing it there. But when I run with mocha, it fails if I import the file.
I can't really pinpoint the error. It seems just like a bug in mocha, but maybe I'm doing something "bad" by exporting the same object that I import, and node is just more forgiving?
I'm using the latest version of mocha (1.13.0). Thanks!
Edit:
This doesn't fix the error, and is not ideal syntax-wise:
person = require 'person'
class driver
constructor: ->
person.setSkill "Drive"
#person = person
module.exports = driver
Note that simply wrapping it in a plain object doesn't work.
Edit 2:
Here's something else that doesn't work:
configure-driver.coffee
configureDriver = (person) ->
person.setSkill "Drive"
module.exports = configureDriver
car.coffee
driver = require('./configure-driver')(require 'person')
Mocha throws the same error as before.
Maybe a little late but hopefully it will help someone (I just spent an hour paging through mocha's source code to track this down).
Try that command instead (the important bit is the equal sign after --compilers):
mocha --compilers=coffee:coffee-script --reporter spec ./test/unit/*-test.coffee
I ran into that bug while trying to create a new grunt test taks using grunt-mocha-istanbul and coffeescript test definitions. Strangely, if I ran the command directly in my shell it worked but using the grunt task I got the same error as you did.
It seems Mocha uses commander and it's global. In my case I had a script under the test directory that uses commander. It looks like Mocha executes the test scripts, parses mocha.opts, and then executes the specs. To fix it I just moved the scripts using commander out of the test dir and all was good.

Resources