OS-independent grunt script command in NPM package.json - windows

When the command grunt is run in a Windows shell in the same path as the grunt.js file, Windows opts to run grunt.js using the Windows Script Host. The recommended path is to explicitly call grunt.cmd.
That's all fine and dandy, but what can I do if I want to create an OS-independent script command in my NPM package.json? I can't do this if I want to also run in *nix:
"scripts": {
"dox": "grunt.cmd dox"
}
Any pointers? Or, is there a big piece of the puzzle that I'm missing? (I'm equally new to both NPM and Grunt).

I've found the solution in creating a grunt.bat file that just calls grunt.cmd:
#grunt.cmd %*
Therefore, on Windows, just invoking grunt triggers the batch file since it gets higher priority than Windows Scripting Host's picking up of grunt.js. On Linux, the regular grunt binary gets picked up. This works cross-platform for me now.

I've got the same problem: when there is tslint.js file in the root dir of the package and an npm script in package.json just calls tslint, jscript is fired by npm instead of node, resulting in "Microsoft JScript compilation error" popup.
A workaround is removing .JS from PATHEXT environment variable.

Related

Windows Script Host Error: Invalid character - Code:800A03F6 - Source: Microsoft JScript compilation error

I'm having an issue with Powershell and CMD. When I try to execute Angular CLI commands in CMD like ng --version or ng new projectName, I get this error;
Windows Script Host Error: Invalid character
Code: 800A03F6
Source: Microsoft JScript compilation error
Update:
On Windows .js files are associated to Windows Scripting Host by default, so the script will not be run with Node.
Open a file explorer and find a JavaScript file, open the JavaScript file's properties and then "open with", select the Node.js program file to open that kind of files.
The error should stop after doing this.
This is how I solved it: (on windows 10)
Go to C:\Users\<your_username>\AppData\Roaming\npm\node_modules\#angular\cli\bin
Check for ng.js
Right click on ng.js file and click on "properties" option
You need to open it with node.exe so click on "Change" button go to node js installed directory and
(example: C:\Program Files\nodejs\node.exe)
Select node.exe
Click on OK
It should change the color of ng.js like below:
Now try ng -v and other ng commands
Installing this exact Angular version:
npm -g install #angular/cli#10.3.1
instead of the latest version:
npm -g install #angular/cli
fixed the above error.
I ran into this exact issue after updating to Angular CLI 13. Tried tons of different suggestions from other threads. What is described in the solutions here is essentially what worked for me, but I just want to point out a possible alternative method to applying the fix that doesn't associate all JS files with node.js.
Trying to execute a script from package.json on Windows throws a JScript error
In your windows system environment variables is one variable called PATHEXT. If the value contains .JS;, remove it. Then restart your CMD windows.
make sure you have proper path variable configured as shown below
Go to your system variable settings
path variable snapshot
make sure you have all these mentioned as part of path
C:\Users<userfolder>\AppData\Roaming\npm\node_modules#angular\cli
C:\Users<userfolder>\AppData\Roaming\npm
C:\Program Files\nodejs
make sure you have all these mentioned as part of path C:\Users\AppData\Roaming\npm\node_modules#angular\cli C:\Users\AppData\Roaming\npm C:\Program Files\nodejs
in my case, before npm install -g #angular/cli, the path of my system variable was:
C:\Users\AppData\Roaming\npm\node_modules#angular\cli\bin
I remove \bin and work!!!!!
Remember to fix this for the correct User
Associating .JS files to node.exe is the way to solve this.
BUT after struggling with the same issue, I wanted to add that the file association needs to be done with the same USER that you are working with the Terminal/Shell.
So if you use the Terminal as a Admin, you must login with your Admin Account just to fix the file association.
Cheers

Cloudflare Wrangler Installation

I am currently using MacOS and am trying to install wrangler using npm i #cloudflare/wrangler -g .
It tells me that Wrangler was installed correctly, but when I attempt to use wrangler it tells me zsh: command not found: wrangler.
How can I fix this?
Thank you
Have you solved your problem?
First of all: npm adds to $PATH the folder with binaries or scripts to launch them.
In windows it's ~\AppData\Roaming\npm, on linux it's /tools/node/bin etc.
Check your $PATH variable for something like "node" or "npm" or run 'npm list -g --depth=0' - the path to target location will be on first line of output.
If other globally installed packages works fine then check if npm folder contain any script related to wrangler. Maybe the problem is that wrangler was not installed correctly and related script was not added. Then try to reinstall. Or probably you can write this script by yourself.
Finaly note that wrangler binary(not script to launch it) is installed to ~/.wrangler folder (according to node_modules/#cloudflare/wrangler/binary.js)
If you will try to make your own script to run that you need:
run npm list -g --depth=0 and get the path from first line of output
find there node_modules
run cd node_modules/#cloudflare/wrangler
make sure that running node run-wrangler.js will not throw any errors (output is just wrangler "how to use")
if any errors occurs: try to node install-wrangler.js - this should download wrangler binary to ~/.wrangler and retry step 4
when everything is ready, go to path where scripts for globally installed packages located and add here your script which will at least run command node /path/to/the/run-wrangler.js and passing your args to it.

Why does running npm test result in: '.' is not recognized as an internal or external command, operable program or batch file.?

I have the following installed:
Windows 10
Git bash (mingw64)
Node.js v8.7.0
npm version 5.4.2
Packages:
chai 4.4.1
mocha 3.5.0
I have a sample mocha test that will always pass when it actually runs.
The command I'm running in my shell:
npm test
Output:
./node_modules/mocha/bin/_mocha
'.' is not recognized as an internal or external command,
operable program or batch file.
npm ERR! Test failed. See above for more details.
For some reason I'm able to run this command directly:
./node_modules/mocha/bin/_mocha
Which results in
Sample Test
√ passes!
1 passing (4ms)
I'm guessing this has something to do with the weirdness of using a bash-like shell inside of windows, however I'm not sure how I can troubleshoot this from here.
Why might this error be happening, and is there a way to get 'npm test' to work properly without having to ditch using this windows programming environment?
Thanks & Regards
This may not be a perfect answer, but it fixes the issue in a way that is sufficient to continue developing:
in my package.json file I had:
"test" : "./node_modules/mocha/bin/_mocha"
Changing this to:
"test" : "node ./node_modules/mocha/bin/_mocha"
Made it so I could run npm test command fine.
I'm still unsure as to why the original value would not work since running that as a command in the shell works just fine.
If anyone sees this and wants to offer some insight that would be greatly appreciated.

How can I keep grunt separate from my source files?

I've spent about a day trying to figure this out.
What I finally did was a bash script:
gw() {
# runs grunt using this path
cd ~/root_install/grunt
grunt watch &
cd ~/root
}
My install file are in root_install and my source file are in root.
Note installing the grunt task runner globally is deprecated.
Note that setting the location of the Gruntfile.js does not work as it appears to need a local installation of the task runner.
This seems to be the deal killer.
But alas, application files I want separate from my source files and Grunt is an application.
What I have works, but it is a bit hackish as I don't want to spend any more time on this.

How to use npm with node.exe?

I have downloaded Windows Binary (.exe) of nodejs from the main page.
How can I install and use npm (Node package manager)?
The current windows installer from nodejs.org as of v0.6.11 (2012-02-20) will install NPM along with NodeJS.
NOTES:
At this point, the 64-bit version is your best bet
The install path for 32-bit node is "Program Files (x86)" in 64-bit windows.
You may also need to add quotes to the path statement in environment variables, this only seems to be in some cases that I've seen.
In Windows, the global install path is actually in your user's profile directory
%USERPROFILE%\AppData\Roaming\npm
%USERPROFILE%\AppData\Roaming\npm-cache
WARNING: If you're doing timed events or other automation as a different user, make sure you run npm install as that user. Some modules/utilities should be installed globally.
INSTALLER BUGS: You may have to create these directories or add the ...\npm directory to your users path yourself.
To change the "global" location for all users to a more appropriate shared global location %ALLUSERSPROFILE%\(npm|npm-cache) (do this as an administrator):
create an [NODE_INSTALL_PATH]\etc\ directory
this is needed before you try npm config --global ... actions
create the global (admin) location(s) for npm modules
C:\ProgramData\npm-cache - npm modules will go here
C:\ProgramData\npm - binary scripts for globally installed modules will go here
C:\ProgramData\npm\node_modules - globally installed modules will go here
set the permissions appropriately
administrators: modify
authenticated users: read/execute
Set global configuration settings (Administrator Command Prompt)
npm config --global set prefix "C:\ProgramData\npm"
npm config --global set cache "C:\ProgramData\npm-cache"
Add C:\ProgramData\npm to your System's Path environment variable
If you want to change your user's "global" location to %LOCALAPPDATA%\(npm|npm-cache) path instead:
Create the necessary directories
C:\Users\YOURNAME\AppData\Local\npm-cache - npm modules will go here
C:\Users\YOURNAME\AppData\Local\npm - binary scripts for installed modules will go here
C:\Users\YOURNAME\AppData\Local\npm\node_modules - globally installed modules will go here
Configure npm
npm config set prefix "C:\Users\YOURNAME\AppData\Local\npm"
npm config set cache "C:\Users\YOURNAME\AppData\Local\npm-cache"
Add the new npm path to your environment's PATH.
setx PATH "%PATH%;C:\Users\YOURNAME\AppData\Local\npm"
For beginners, some of the npm modules I've made the most use of are as follows.
axios - for more complex http posts/gets
isomorphic-fetch - for http(s) post/get requests
node-mailer - smtp client
mssql - interface and driver library for querying MS SQL Server (wraps tedious)
More advanced JS options...
async/await - async functions, supported via babel
For testing, I reach for the following tools...
mocha - testing framework
chai - assertion library, I like chai.expect
sinon - spies and stubs and shims
sinon-chai - extend chai with sinon's assertion tools
babel-istanbul - coverage reports
jest - parallel testing, assertions, mocking, coverage reports in one tool
babel-plugin-rewire - slightly easier for some mocking conditions vs. jest
Web tooling.
webpack - module bundler, package node-style modules for browser usage
babel - convert modern JS (ES2015+) syntax for your deployment environment.
If you build it...
shelljs - shell utilities for node scripts,. I used to use gulp/grunt, but these days will have a scripts directory that's referenced in package.json scripts via npm. You can use gulp tools inside plain scripts.
When Node.js is not installed using the msi installer, npm needs to be setup manually.
setting up npm
First, let's say we have the node.exe file located in the folder c:\nodejs. Now to setup npm-
Download the latest npm release from GitHub (https://github.com/npm/npm/releases)
Create folders c:\nodejs\node_modules and c:\nodejs\node_modules\npm
Unzip the downloaded zip file in c:\nodejs\node_modules\npm folder
Copy npm and npm.cmd files from c:\nodejs\node_modules\npm\bin to c:\nodejs folder
In order to test npm, open cmd.exe change working directory to c:\nodejs and type npm --version. You will see the version of npm if it is setup correctly.
Once setup is done, it can be used to install/uninstall packages locally or globally. For more information on using npm visit https://docs.npmjs.com/.
As the final step you can add node's folder path c:\nodejs to the path environment variable so that you don't have to specify full path when running node.exe and npm at command prompt.
npm can be downloaded (without installation) from here:
http://nodejs.org/dist/npm/
https://github.com/npm/npm/releases
I just installed latest version of node (0.6.12) in Windows 7 using msi (node-v0.6.12.msi).
npm is already shipped with it, no need to include it separately.
I was facing permission issue while running npm (npm install mysql), from the path where my nodejs resided, i.e.
C:\Program Files (x86)\nodejs
Then I followed below steps:
1) Added C:\Program Files (x86)\nodejs\npm in environment variables - Path system variable.
2) went back to only C:\ in command prompt and gave the command - npm install mysql - and voila! it worked..
Hope this helps.
I am running node.js on Windows with npm.
The trick is simply use cygwin. I followed the howto under https://github.com/joyent/node/wiki/Building-node.js-on-Cygwin-(Windows) . But make sure that you use version 0.4.11 of nodejs or npm will fail!
I've just installed 64 bit Node.js v0.12.0 for Windows 8.1 from here.
It's about 8MB and since it's an MSI you just double click to launch. It will automatically set up your environment paths etc.
Then to get the command line it's just [Win-Key]+[S] for search and then enter "node.js" as your search phrase.
Choose the Node.js Command Prompt entry NOT the Node.js entry.
Both will given you a command prompt but only the former will actually work. npm is built into that download so then just npm -whatever at prompt.
Use a Windows Package manager like chocolatey. First install chocolatey as indicated on it's homepage. That should be a breeze
Then, to install Node JS (Install), run the following command from the command line or from PowerShell:
C:> cinst nodejs.install
Here is a guide by #CTS_AE on how to use NPM with standalone node.exe:
https://stackoverflow.com/a/31148216/228508
Download the node.exe stand-alone from nodejs.org
Grab an NPM release zip off of github https://github.com/npm/npm/releases
Create a folder named: node_modules in the same folder as node.exe
Extract the NPM zip into the node_modules folder
Rename the extracted npm folder to npm and remove any versioning ie: npm-3.3.4 –> npm.
Copy npm.cmd out of the /npm/bin/ folder into the root folder with node.exe
I just installed Node.js for the first time and it includes NPM, which can be ran from the Windows cmd. However, make sure that you run it as an administrator. Right click on cmd and choose "run as administrator". This allowed me to call npm commands.
Search all .npmrc file in your system.
Please verify that the path you have given is correct. If not please remove the incorrect path.

Resources