ken ken - 17 days ago 9
TypeScript Question

"npm run CMD" not working

I may be being an "npm numpty" but I can not figure out why when I type:

tslint --force --format verbose \"src/**/*.ts\"


it works fine but when I type:

npm run lint


where my
package.json
includes:

"scripts": {
"clean": "rimraf lib",
"lint": "tslint --force --format verbose \"src/**/*.ts\"",
"build": "npm run clean && npm run lint && echo Using TypeScript && tsc --version && tsc --pretty",
"test": "npm run build && mocha --compilers ts:ts-node/register --recursive test/**/*-spec.ts",
"watch": "npm run build -- --watch",
"watch:test": "npm run test -- --watch"
},


I get the following error:


module.js:471
throw err;
^

Error: Cannot find module './test/parse'
at Function.Module._resolveFilename (module.js:469:15)
at Function.Module._load (module.js:417:25)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object. (/Volumes/PEGASUS/repos/mine/logger/serverless-event/node_modules/tslint/lib/test.js:10:13)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)


I also looked in the
npm-debug.log
file and amongst other things it stated:

23 error Tell the author that this fails on your system:
23 error tslint --force --format verbose "src/**/*.ts"


Of course that precise command does not fail which is the whole issue I'm having.




Of note, I did see this similar sounding issue -- npm run cmd fails while cmd on command line works --
but the discussed solution makes no difference to the error I'm getting.




Also of note, I currently have no linting errors, warnings, or anything to which I'd expect it return an error status.

Answer

The confusion is likely to be because npm scripts add to your PATH, to include ./node_modules/.bin. That means when you use tslint in your package.json, you're referencing the locally installed version in node_modules, and when you just use tslint directly you're using the globally installed version. Seems like only the globally installed version is working.

You can confirm this by explicitly specifying the path on the command line, i.e. running ./node_modules/.bin/tslint --force --format verbose \"src/**/*.ts\". That directly uses the node_modules version, and should give you the same failing result you're seeing with your NPM script.

That doesn't solve your underlying problem though. It sounds like your locally installed tslint is failing to find a file it uses, although I don't know why. Unless there's an obvious cause for this, I think the quickest solution is probably to just delete your node_modules folder and npm install again, to pull down a new working version.

A fresh install of tslint with all its dependencies should be able to successfully load all its dependencies, and you shouldn't see these issues. If it can't then you probably have a bug to file with tslint.