20

New Grunt user here who is using a lot of new tools (npm nodejs) today.

I've got Grunt "installed" and have been able to create a grunt.js file using the init task as described here: http://net.tutsplus.com/tutorials/javascript-ajax/meeting-grunt-the-build-tool-for-javascript/ and here: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md. But whenever I run the "grunt" command I get an error:

Windows Script Host
Script: c:\users\[]\Documents\code\grunt\grunt.js
Line: 2
Char: 1
Error: 'module' is undefined
Code: 800A1391
Source: Microsoft JScript runtime error

10 Answers 10

24

As explained in the FAQ, you need to type grunt.cmd instead on Windows because the OS tries to launch grunt.js

Or you can install grunt-cli globally instead. This package will run any version of Grunt if it's been installed locally to your project.

2
  • Where is grunt.cmd? I can't find any file by that name in the entire Grunt repository.
    – Jonathan
    Commented Aug 31, 2012 at 4:13
  • 3
    I can't find a grunt.cmd anywhere on my system. I found a C:\Documents and Settings\myuserid\Application Data\npm\node_modules\grunt directory but there is no grunt.cmd in there, just a grunt.js which seems to be the problem. Commented Feb 21, 2013 at 20:21
5

SOLVED !!

So, this problem occur because windows by default associative < *.js > files with >> "Microsoft Windows Based Script Host".

grunt need to open by default with (grunt.cmd).

it easy to slove, by change default app (open with..)


Guide :

  1. Go to any javascript file with "js" extension. (any file)

  2. Right-Click(mouse) > Properties > "Opens with:" Change...(button)

  3. Choose Notepad ( or any javascript IDE ).

PROBLEM SOLVED ! :) good luck

1
  • Thank you for sharing, I nearly desperated on this problem. Commented Jul 8, 2020 at 16:41
3

If you're getting a "Microsoft JScript runtime error" that means that node.js isn't even getting invoked; instead Windows Script Host is trying to run your code. That's probably a problem with filetype associations; IIRC Windows defaults to trying to execute a ".js" file with WSH. You may wind up having to create a shortcut to your script, specifying a command line (probably something like "node %1") and a starting directory in order to make sure that it's executed properly.

It would help if you could tell us exactly how you're trying to invoke your code.

3

it seems that in the latest versions of the grunt modules, you would have to do the following to have it work under windows:

remove any globally installed grunt

npm uninstall -g grunt

install grunt-cli globally

npm install -g grunt-cli

install grunt locally into your project

npm install grunt

installing grunt (v0.4.x) globally does not seem to create the necessary grunt.cmd anymore. it seems that the recommendation is now to have grunt installed locally to be able to use version-specific Gruntfiles

2

As Florian F suggested, running grunt.cmd works. This is because of the process Windows is looking for your grunt command.

When typing grunt -h Windows will proceed to look for the following files:

  1. ./grunt.cmd
  2. ./grunt.* (grunt.js is found in this case which is why you see "module is undefined")
  3. %APPDATA%/npm/grunt.cmd

An alternative to using "grunt.cmd" is to use grunter which simply renames the command to grunter... then you no longer have this problem.

0
2

To answer this, first we need to understand that the error is caused because it is being executed by Windows Script Host.

Now, run the code from your cmd promt with the following syntax:

>node <application_name>.js

this will allow the Node.js application to open through V8 JavaScript engine(Google's).

P.S: Please reply back if this has helped in resolving your issue else post the problem you are facing after trying this.

2
  • This question is 4 years old and already been answered well. You may want to consider deleting your answer Commented Nov 15, 2016 at 5:52
  • @JamesJones oh..! then i am happy the issue has been resolved.. :-) But i am always ready to solve the issues which i know solutions for..
    – Arjun
    Commented Nov 15, 2016 at 9:55
1

I had a similar issue, the problem is file association, I would recommend:

  • right click on a .js file and choose open with.
  • then you choose nodejs/node.exe (somewhere in "program files" folder
  • then make tick box where it says "always open .js files " (paraphrasing)

That should do the trick.

1

I went through the same issue when running an old Node project.

The issue was with the name of the js file, it was node.js. So the while running the command node node.js, it was opening up a windows dialogue box.

I just changed the name of the file to app.js and the error flew away.

0

So, in my case i had tryed all the mentioned above with no result. But i have fund that im dont type: node in the full sentence as the following snipet node script.js.And remember never understimate your own miscoding.

2
  • Isn't that what @Arjun said above?
    – Stanislas
    Commented Aug 28, 2020 at 22:57
  • I have tryed all the first ones, but simply did a mistake in console sentence Commented Aug 29, 2020 at 23:18
0

Solution:

  1. Go to any javascript file with "js" extension. (any file)

  2. Right-Click(mouse) > Properties > "Opens with:" Change...(button)

  3. Choose Notepad ( or any Javascript IDE like VS Code ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.