I think npm install|update works when requiring dependencies in source files, but when I want to run an executable, like nodemon, it doesn’t appear to work. Is it trying to look for the file globally? How can I make these commands look in node_modules first?
I have a Cakefile that starts the dev server with nodemon. For example:
# **`procExec(procName)`**
# returns the path to executable in `node_`
procExec = (procName) ->
console.log "./node_modules/" + procName + "/bin/" + procName
"./node_modules/.bin/" + procName
# **`cake startdev`**
# Starts the server with `nodemon`
# Watch and compile `.coffee` and `.styl` files in `/client`
task "startdev", "Starts server with nodemon and watch files for changes", ->
# start nodemon server
nodemon = spawn procExec("nodemon"), ["server.coffee"]
processOutput nodemon
# watch and compile CoffeeScript
coffee = spawn procExec("coffee"), ["-o", "public/js/app", "-cw", "client/coffee"]
processOutput coffee
# watch and compile Stylus
stylus = spawn procExec("stylus"), ["client/stylus", "-l", "-w", "-o", "public/css/app"]
processOutput stylus
It works but with a few minor problems:
npm install|updatedoesn’t seem to installnodemon. I think it tries to install globally and fails. I manually did anpm install nodemonseparately. Why is this? And how can I tellnodemonto install anyway?- Does
"./node_modules/.bin/" + procNamealways resolve to the correct executable?
There’s a few questions in here, so I’ll try and keep them separated.
Did you see a warning about “prefer global install”? If that’s the case it was just a warning and it would’ve been installed anyways. If it was a different error please include the output.
Yes, any scripts listed in the package.json files of your dependencies will be installed to this folder. However, I much prefer using the
npm bincommand to always get the correct path.If you are spawning processes from node, you can also
require('npm')and modifyprocess.env.PATHto get the correctnode_modules/.binin place. E.g. at the top of your Cakefile:disclaimer I don’t know if modifying PATH like that will work on windows.