standard / ts-standard

Typescript style guide, linter, and formatter using StandardJS
https://www.npmjs.com/package/ts-standard
MIT License
473 stars 36 forks source link

Unexpected linter output #35

Closed ghost closed 4 years ago

ghost commented 4 years ago

Hey guys,

when running npx ts-standard --fix in my ts-project, I am getting:

Failed to load parser '@typescript-eslint/parser' declared in '--config » eslint-config-standard-with-typescript#overrides[0]': Cannot find module 'typescript'
Require stack:
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/@typescript-eslint/typescript-estree/dist/parser.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/@typescript-eslint/parser/dist/parser.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/config-array-factory.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/cascading-config-array-factory.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/cli-engine.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/index.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/api.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/dist/ts-standard.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/dist/cli.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/bin/cmd.js: Error: Failed to load parser '@typescript-eslint/parser' declared in '--config » eslint-config-standard-with-typescript#overrides[0]': Cannot find module 'typescript'
Require stack:
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/@typescript-eslint/typescript-estree/dist/parser.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/@typescript-eslint/parser/dist/parser.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/config-array-factory.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/cascading-config-array-factory.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/cli-engine.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/cli-engine/index.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/eslint/lib/api.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/dist/ts-standard.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/dist/cli.js
- /Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/bin/cmd.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.<anonymous> (/Users/dominik/.npm/_npx/46101/lib/node_modules/ts-standard/node_modules/@typescript-eslint/typescript-estree/dist/parser.js:20:25)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
toddbluhm commented 4 years ago

The error above looks to be failing because it cannot find your typescript module. Do you have typescript installed in the project you are running this on? ts-standard does not install typescript for you, but rather declares it as a peerDependency (meaning you have to install it manually yourself).

ghost commented 4 years ago

This is my package.json. So I guess, it should work.

https://github.com/Syy0n/serverless-blueprint/blob/master/package.json

ghost commented 4 years ago

Adding it as devDependency solved the problem. But via npx it is not working.

toddbluhm commented 4 years ago

I kind of figured that would be the case (you already have it installed), but I like to start simple and work towards complex 😉

So I just pulled down that repo, and cd into the root directory (where package.json was). I then performed the following steps:

  1. Deleted the //1, //2, etc... stuff in the package.json
  2. Ran npm install
  3. Ran npm run build
  4. Ran npx ts-standard --fix

Everything worked great and ts-standard ran as expected. I also used a fresh node installation so that there were no globally install modules.

Try deleting your node_modules folder and redoing a full npm install all over again...sometimes things get screwed up in there.

It also might be the //1, //2 etc in your package.json as a lot of tools will read that file and if it does not contain valid JSON it might screw with how the tool operates.

If you are still running into issues, try recreating a minimal repo example with a single typescript file and a package.json with typescript and ts-standard and see if it continues to happen there.

toddbluhm commented 4 years ago

Adding it as devDependency solved the problem. But via npx it is not working.

☝️ That was super helpful. I forget that people run npx ... without actually installing the module they are running npx on. I just hit the same error. It is very interesting...will look into it, but I am guessing this is actually an issue with how @typescript-eslint/parser actually finds and uses the typescript package

toddbluhm commented 4 years ago

After some additional research, it seems to be an issue with npx itself and how it resolves node_modules. From what I can tell, npx does not install the module globally or locally, but instead, in some weird cache location that does not have access (or maybe a bug) to either global or local node_modules.

For now, the only solution is to make sure ts-standard is installed locally before running npx ts-standard.

Interestingly enough, this issue might be solved by npm@7 because it will install peerDependencies by default thus, wherever npx installs ts-standard it should also install typescript too.

Closing this as it is not fixable by this project.