Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module '<tsconfig from node_modules>' #682

Closed
jonahsnider opened this issue Aug 25, 2022 · 2 comments · Fixed by #683
Closed

Error: Cannot find module '<tsconfig from node_modules>' #682

jonahsnider opened this issue Aug 25, 2022 · 2 comments · Fixed by #683

Comments

@jonahsnider
Copy link
Contributor

Bug description

When I run xo I get this error:

Error: Cannot find module '@namespace/package'
Require stack:
- /Users/jonah/programming/xo-bug/tsconfig.json
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at recursiveBuildTsConfig (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:649:27)
    at handleTSConfig (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:180:28)
    at async mergeWithFileConfig (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:139:13)
    at async parseOptions (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:586:51)
    at async Promise.all (index 0)
    at async getOptionGroups (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:602:21)
    at async Object.lintFiles (file:///Users/jonah/programming/xo-bug/node_modules/xo/index.js:78:17)
    at async file:///Users/jonah/programming/xo-bug/node_modules/xo/cli.js:211:18

This is my tsconfig.json:

{
	"compilerOptions": {
		"declaration": true,
		"inlineSourceMap": true,
		"moduleResolution": "node",
		"outDir": "dist"
	},
	"extends": "@tsconfig/node16",
	"include": ["src"]
}

Bug reproduction

Here's a fully reproducible project:
xo-bug.zip

  1. Download & extract
  2. yarn
  3. yarn test
  4. Error occurs

Fix

I believe this is a bug introduced in #677.

The fix is probably replacing this:

xo/lib/options-manager.js

Lines 642 to 644 in c8ec522

if (!tsConfig.extends || (typeof tsConfig.extends === 'string' && tsConfig.extends.includes('node_modules'))) {
return tsConfig;
}

with something like this:

 if (!tsConfig.extends || (typeof tsConfig.extends === 'string' && tsConfig.extends.includes('node_modules') || tsConfig.startsWith('@'))) { 
 	return tsConfig; 
 } 
@spence-s
Copy link
Contributor

@jonahsnider thanks for the report and the repro!

I think we've nearly got the tsconfig changes sorted out now, hate that it introduced so many regressions...

Unfortunately @tsconfig doesn't add a main field to the package.json which is why require.resolve can't find the config, but ultimately we should support whatever TS does as close as possible...

I don't think the check for startsWith('@') will fix this entirely, since it would be possible to install a tsconfig from npm that isn't prefixed with @ that also doesn't have the main field of package.json listed as the tsconfig.

I will make a PR for a different patch that will support this syntax and resolution logic to assume that tsconfig.json is always the name of the file it is looking for.

@sindresorhus
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants