I'm doing a PoC of NPM as a build tool (http://blog.keithcirkel.co.uk/how-to-use-npm-as-a-build-tool/). I'm fairly new using NPM. For now, I only have JSHint and Mocha installed. My packagae.json is attached. Now, when I run "npm run lint" in the command line (Windows 7), it gives me an error:
c:\project>npm run list
MyNPMProject#1.0.0 lint c:\project
jshint test/*.js
ERROR: Can't open test/*.js
It works when I change the script "lint": "jshint test/test.js".
Can I use glob with jshint?
Please advise and thank you in advanced.
You shouldn't need the glob, just give it the directory and it will scan all js files in there.
If you need to use a wildcard that can recurse down into subfolders, such as test/**.js, the basic Windows shell (Command Prompt) doesn't support that, but there are various workarounds/alternatives. See this for more details:
https://stackoverflow.com/a/30114333/1593924
Related
I have a node project written in typescript#2.
My tsconfig has sourceMap set to true and the *.map.js files are generated. When I execute my transpiled *.js JavaScript files via node or nodemon, I only see the error messages relative to the js file and not to the mapped typescript files; I assume it's completely ignored.
Is sourceMap support only intended for browser-support? Or can I use it together with node or nodemon? If the latter, how would I enable it?
I want to see runtime errors detected from an executed javascript file relative to the original typescript file.
🚩 for Node versions since v12.12, there is an easier and better solution.
I recently got this working in my express app. Steps as follows:
Install the required library:
npm install --save-dev source-map-support
In your entry point (eg app.ts):
require('source-map-support').install();
In your app.ts, you may also require better logging for errors within promises:
process.on('unhandledRejection', console.log);
In your tsconfig, under compilerOptions:
"inlineSourceMap": true
The answers here are correct for Node versions before v12.12.0, which added the (experimental) --enable-source-maps flag. With that enabled, source maps are applied to stack traces without an additional dependency. As demonstrated in this article, it has the slightly different and possibly beneficial behavior of including both the generated .js file location and the source file location. For example:
Error: not found
at Object.<anonymous> (/Users/bencoe/oss/source-map-testing/test.js:29:7)
-> /Users/bencoe/oss/source-map-testing/test.ts:13:7
Install source map support:
npm install source-map-support
(I run in in production as well, as it immensely helps finding bugs from the logs of when an error an occurs. I did not experience a large performance impact, yet your experience may be different.)
Add to your tsconfig.json:
{
"compilerOptions": {
"sourceMap": true
}
}
When running your JavaScript file, add the require parameter:
nodemon -r source-map-support/register dist/pathToJson.js
node -r source-map-support/register dist/pathToJson.js
Alternatively, you add in your entry call:
require('source-map-support').install()
yet I find this tedious is projects with multiple entry points.
Sidenote: mocha also supports the --require / -r option, so to have the sourcemap support in mocha you can also call your tests with it, e.g. similar to:
NODE_ENV=test npx mocha --forbid-only --require source-map-support/register --exit --recursive ./path/to/your/tests/
I found this npm module which seems to do the trick:
https://github.com/evanw/node-source-map-support
run npm install source-map-support --save at the root of your node project and add import 'source-map-support/register' to your main.ts or index.ts file.
That's it.
Source map support works perfectly fine with node
All you need to do is add
"source-map-support": "0.4.11",
to dependencies or dev-dependencies in package.json by running
npm install --save source-map-support
And in your entry point ts file, simply add at the top
require('source-map-support').install()
(note: this is calling nodeJS require - there is no need for source-map-support definition files)
For Node versions from v12.12.0 use the --enable-source-maps flag when you run node.
Example: node --enable-source-maps main.js
Do not install "source-map-support" for Node versions from v12.12.0
I cant get my head around how scripts are running within package.json & would appreciate some insight for us newbies.
Is it the case that they are bash scripts that are run by node having loaded the various dependencies?
If yes, then how does it process the javascript code?
Is it the case that they are bash scripts
yes
that are run by node
no, they are run by sh.
having loaded the various dependencies?
no, no js files are loaded, the only thing npm does for you is to prepare the environment. Among other things, it adds ./node_modules/.bin to PATH so you can invoke installed modules immediately.
When you run npm run-script whatever, this is what npm does:
reads the corresponding command line from package.json
prepares the environment
invokes sh (or comspec on win) and gives it the command and the env. No big magic here.
This may not be 100% accurate so I implore other, more qualifies, experts to chime in.
NPM is a program, installed as part of the Node.JS environment. It's two main uses (as describe here) are for searching for node.js packages and installing node.js packages.
However, NPM is also capable of understanding "simple" (a relative term) scripts.
When you write a script in your package.json, and issue the NPM command, say "npm start", NPM will read and interpret the script. NPM then searches your node_modules structure for the accompanying binary and executes that binary with the necessary start parameters.
An example would be
"test": "mocha --reporter spec test"
when you issue "npm test", NPM will look for the mocha binary in your node_modules structure. NPM finds mocha initiates the call, passing the reporter command arg (--reporter spec) and the name of the file to be read and executed for the test.
I'm trying to get grunt.js set up on my work machine. Now I've managed to get it set up at home, so I pushed my repository, then cloned it on my work machine, however despite troubleshooting this to death I've always run into the same issue when I come to try and run the command on my work computer;
sh.exe": grunt: command not found
So some background and explanation;
I'm using Aptana 3.0 and running all my commands through the terminal. (This applies to my home and work computer)
I cloned the "working" repository and put it in a folder on my work machine, so I have my package.json and gruntfile.js files inside said directory. These work fine on my home computer and I am able to run the watch task set up in my gruntfile.js successfully at home.
I've run npm install grunt-cli -g and npm install inside my project on my WORK computer and installation has been successful each time. My only error messages on either are a lack of description and a repository field which, to my knowledge these aren't mandatory?
The node modules folder inside my repository indicates I have the following installed (as per my package.json)
grunt
grunt-contrib-compass
grunt-contrib-uglify
grunt-contrib-watch
matchdep
Now I'm pretty green to this command line stuff, so I may have overlooked something very obvious but I feel like I've tried every guide going to get this thing to work.
Despite running through the getting started steps/installing grunt documentation repeatedly, it seems no matter what I do the terminal will not pick up grunt as a recognized command.
Am I stupid or is this some other issue?
EDIT 1: contents of my package.json:
{
"name" : "xxxxxxxxx",
"version" : "xxxx",
"dependencies" : {
"grunt":"~0.4.1",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-compass": "~0.5.0",
"grunt-contrib-uglify": "~0.2.2",
"matchdep": "~0.1.2"
}
}
This is happening because you are using the Aptana Terminal, which needs the PATH variable to work out what is meant by grunt etc. A normal cmd prompt would work fine with the command npm install grunt-cli -g but in this context the terminal is unaware of what grunt is.
As per the following existing answer, you need to set up your Windows PATH variable to make the Aptana Terminal aware of the npm directory: https://stackoverflow.com/a/19137584/463205
C:\Users\Username\AppData\Roaming\npm
Closing the terminal and reopening it after setting the PATH correctly should enable you to run the command successfully.
Try to run
npm install grunt-cli -g
On your home computer.
-g means - install Grunt globally (not in the project node_modules folder), so it'll add grunt command to the bin folder which is used by nodejs console ( and you need to run node.js command prompt, not just arbitrary cmd)
Quick question, when I run browserify index.js -o app.js from mac terminal, I get command not found. I have done npm install -g browserify but still no luck. Any idea why I am getting this?
Thank you
It was easier for me to do a gist than to paste here:
https://gist.github.com/pertrai1/4ccf77e7b31cb5628b5d
Just install it in a global space like this if you need to run it from the command line.
npm install browserify -g
You may need to run
npm uninstall browserify -g fist just to be sure you don't have false aliases.
Add this to your ~/.bashrc or equivalent:
export PATH=$PATH:~/.npm-global/bin/
Then, to actually have this take effect in your terminal session, execute source ~/.bashrc.
At this point you can execute browserify, as well as potentially many other commands. Check out ~/.npm-global/bin/ to see what's become available.
I could not get browserify to work either.
Running ~/.npm/bin/browserify does work.
Other packages seem to run fine (phantomjs for instance).
A workaround fix seems to be adding alias browserify='~/.npm/bin/browserify' to your .bash_profile
It is an old post but I believe people are still facing the same problem, like me.
This was how I solved my problem:
<your project folder>/node_modules/browserify/bin/cmd.js main.js -o bundle.js
If you install locally npm install browserify, you can use this method to execute the browserify command.
node_modules/.bin/browserify
For example:
broweserify command example
Extra tips:
Add this command to your packages.js file:
...
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"bundle": "node_modules/.bin/browserify index.js > bundle.js"
},
Then everytime you want to bundle you file just hit npm run bundle
Hope it helps you guys out there..
If for some reason the browserify command has not been installed at all (can happen for example if you're running Homebrew on old unsupported Mac OS X versions), an alternative is to call it via node, for example:
export NODE_PATH=/usr/local/share/npm/lib/node_modules
node -e 'require("browserify")("input.js").bundle().pipe(fs.createWriteStream("output.js"))'
As a Mac user I had to add
export PATH=$PATH:/usr/local/Cellar/node/13.6.0/bin/
in ~.bash_profile
Why Cellar path i dont know.
I would like to use nodemon from within the WebStorm IDE (version 7). Nodemon watches one or more files in my source folder and restarts the node process (an Express server in this case), when one of the source files changes.
How do I configure WebStorm to use nodemon in a Run Configuration, so that the node process is automatically restarted?
Without nodemon, I use the following configuration in WebStorm, but have to restart the node process whenever I change something in the source file:
Node interpreter: /usr/local/bin/node
Working directory: /Users/foo/test
JavaScript file: server.js
This results in a Run Configuration that runs node server.js in the specified directory.
From command line, I can use the following command to use nodemon to watch for file changes: nodemon server.js in the project directory.
How do I need to change the WebStorm configuration so that it also uses nodemon?
It looks like the workaround with --exec isn't necessary anymore, at least when using the newest version of nodemon and Webstorm 7 or 8.
All you have to do is specify your path to nodemon by obtaining its path with running which nodemon in your console (e.g. /usr/local/bin/nodemon) under "Node parameters":
#Bela Clark, thanks for confirming.
You may NOT have nodemon exists from which nodemon command, then you should have it in your package.json ie nodemon be installed at :project_dir/node_modules/.bin/nodemon
Then from Webstorm 's run/debug config, set Node parameters to be
:path_to_project_dir/node_modules/.bin/nodemon
You should save the debug/run config to file so your teammates can also easily debug/run your nodejs app like you
This will save the config into some .xml file, sample as below
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="index.js" type="NodeJSConfigurationType" path-to-node="$USER_HOME$/.nvm/versions/node/v19.4.0/bin/node" nameIsGenerated="true" node-parameters="../node_modules/.bin/nodemon" path-to-js-file="index.js" working-dir="$PROJECT_DIR$/nodejs27/node27_sequelize_apiapp/src">
<method v="2" />
</configuration>
</component>
This is the Windows solution
You can just use the nodemon.cmd instead of node directly like :
Node interpreter : C:\MyPath\To\nodemon.cmd
Node parameters : /*Empty for me*/
Node WorkingDirectoy : C:\Users\MyUserName\Desktop\DirectoryContainingMyIndex.js
JavaScriptFile : app\index.js /*or just index.js depending on your config*/
and then :
Hope it will help you.
To install nodemon, use the following (if required, use sudo to run the installation with root privileges:
npm install -g nodemon
This will install nodemon globally on your machine.
Then, in your WebStorm Run Configuration, add the following, leaving everything else unchanged:
Node parameters: /usr/local/bin/nodemon --exec /usr/local/bin/node
This will instruct the node interpreter to execute the nodemon script using the following command line: node /usr/local/bin/nodemon --exec /usr/local/bin/node server.js.
The --exec part is important, as the execution will fail with the following error:
/usr/local/bin/node /usr/local/bin/nodemon server.js
4 Oct 13:56:50 - [nodemon] v0.7.10
4 Oct 13:56:50 - [nodemon] to restart at any time, enter `rs`
4 Oct 13:56:50 - [nodemon] watching: /Users/foo/test
execvp(): No such file or directory
4 Oct 13:56:50 - [nodemon] starting `node server.js`
4 Oct 13:56:50 - [nodemon] exception in nodemon killing node
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
The error seems to be caused by WebStorm not seeing the node executable on its path.
The fix for this is to specify the location to the node executable using the --exec /usr/local/bin/node parameter.
Using these settings, nodemon works fine when run from a WebStorm Run Configuration.
The same trick might have to be used with some of the tools similar to nodemon, e.g. node-supervisor.
I'm on Windows and for me didn't worked with nodemon (no idea why), but someone from Jetbrains suggested to try with supervisor:
I installed supervisor: npm install supervisor -g
Then find where is supervisor installed, for me was in:
C:\Users\AlinC\AppData\Roaming\npm\node_modules\supervisor\lib\cli-wrapper.js –no-restart-on error
I went back to Intellij: edit configurations -> node parameters -> and added:
C:\Users\AlinC\AppData\Roaming\npm\node_modules\supervisor\lib\cli-wrapper.js –no-restart-on error
For those interested for the solution in Windows 10, here is my configuration. It does not show "Terminate Batch" thing and works perfectly.
You press debug ONCE and than you can save change files whatever and the server will restart in debug mode. All brakepoints are working perfectly
For windows users set:
Node Interpreter: Path of the node.exe i.e. c:\program files\node\node.exe
Node parameter: C:\Users\YOURUSER\AppData\Roaming\npm\node_modules\nodemon\bin\nodemon.js
You can also make it work with nvm and debugging still works.
Tested with Node.js 8.1.0 and Webstorm 2017.2
First make sure you are on the right version (in my case v8.1.0) and install nodemon globally -
nvm use v8.1.0
npm install -g nodemon
Then, open Run/Debug configurations and create a new one with the correct node interpreter.
Node parameters should be:
MAC
/Users/[YOUR_USER]/.nvm/versions/node/v8.1.0/bin/nodemon --inspect=3001
LINUX
/usr/local/nvm/versions/node/v8.1.0/bin/nodemon --inspect=3001
Save and debug respponsibally :)
In case you've installed nodemon like a global library, just set in node parameters:
C:\Users\${yourUser}\AppData\Roaming\npm\node_modules\nodemon\bin\nodemon.js
This is the only thing that worked for me:
Add a new package.json node run script command:
Create an NPM CONFIG (not a node config)
Select "start-watch" as the command
For me this worked for debugging / breakpoints without issues or additional headache.
Here's the configuration that works for me on Windows 7 + WebStorm 8.0.4. If I put nodemon.cmd as the node interpreter I kept getting "Terminate batch job (Y/N)?".
Do a npm install nodmemon -g
Only change the Path to Node to the nodemon.cmd, in my case (C:\Users\Rohit Taneja\AppData\Roaming\npm\nodemon.cmd), you'll also get this path after your installion of nodemon finishes.
You're good to go
some of these answers appear to only work for Mac. For Windows, this configuration seems to work (my user name on Windows 7 is denman).
main.js is the starting point file for my Express application.
Just add new script to package.json called nodemon (or choose your own name)
"scripts": {
...
"nodemon": "nodemon ./bin/www"
}
Then go to Run/Debug Configuration and add npm configuration. Set
Command to "run"
Script to "nodemon" (name you chose in package.json)
I have a development in mac and as OdkoPP indicates I made it work
"scripts": {
"build": "tsc",
"dev": "nodemon src/index.ts --exec ts-node"
},
Run/Debug Configurations npm:
Per #bernhardw comment, as this was the answer for me -
All is needed is /usr/local/bin/nodemon under node parameters
Works for run and debug as it restarts upon changes, but debugging with breakpoint does not work.
Bonus: add -e for more extension e.g /usr/local/bin/nodemon -e js,html,jade
(osx 10.10.5, rubymine 7.1.4)
HTH
npm install -g nodemon
1* goto run->Edit Configurations->Press'+' at left corner and choose Node.js
2* Select Node.js and Press '+'
3* Name as Nodemon, add path in javaScript file: C:\Users\Your_User_Name\AppData\Roaming\npm\node_modules\nodemon\bin\nodemon.js
4* Click Apply and Ok
5* Run the Nodemon
Script in package.json: "start": "nodemon --inspect -r babel-register src",
First pic: Run debug and it will start
Second pic: attaching to existing running node
Here is a fix for an error I was getting...
If you are using a Windows + NodeJS + nodemon.
With an IntelliJ - Run Configuration.
ERROR: starting inspector on failed: address already in use
When I use nodemon version 1.19.1, I get the error.
When I use nodemon version 1.18.11, it works!
Good luck...
This is how I am running
Installed nodemon package
npm install -g nodemon # OR using yarn: yarn global add nodemon
From Webstorm terminal, run
nodemon index.js
This is how it will show running in terminal