"error TS2304: Cannot find name" after updating to 7.0.0

See original GitHub issue

I updated ts-node from 6.2.0 to 7.0.0 and I started to get the following error:

TSError: ⨯ Unable to compile TypeScript:
src/common/atlas/atlas.constants.ts(3,27): error TS2304: Cannot find name 'Tile'.
src/common/atlas/atlas.constants.ts(6,31): error TS2304: Cannot find name 'ITileLayer'.
src/common/atlas/atlas.constants.ts(27,28): error TS2304: Cannot find name 'ITileLayer'.

    at createTSError (/Users/miguel/src/later/node_modules/ts-node/src/index.ts:261:12)
    at getOutput (/Users/miguel/src/later/node_modules/ts-node/src/index.ts:367:40)
    at Object.compile (/Users/miguel/src/later/node_modules/ts-node/src/index.ts:557:11)
    at Module.m._compile (/Users/miguel/src/later/node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at require.extensions.(anonymous function) (/Users/miguel/src/later/node_modules/ts-node/src/index.ts:442:12)
    at Object.nodeDevHook [as .ts] (/Users/miguel/src/later/node_modules/node-dev/lib/hook.js:61:7)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
[ERROR] 11:14:48 TSError

It looks like this version is not able to find my custom type definitions. I recently started to learn typescript so I’m not sure if what I’m doing is good practice, this is my folder structure

image

Y put my types inside *.d.ts files and nothing more, this worked before updating.

Not sure if this can help but here is my .tsconfig

{
	"compilerOptions": {
		"strict": true,
		"alwaysStrict": true,
		"module": "commonjs",
		"moduleResolution": "node",
		"types": ["node", "jest", "pixi.js", "matter-js"],
		"newLine": "LF",
		"outDir": "lib",
		"jsx": "preserve",
		"target": "es2017",
		"lib": [
			"es2017",
			"dom"
		],
		"noUnusedLocals": true,
		"noUnusedParameters": true,
		"noImplicitAny": true,
		"noFallthroughCasesInSwitch": true,
		"experimentalDecorators": true,
		"baseUrl": "./src",
		"paths": {
			"common/*": [
				"./common/*"
			],
			"client/*": [
				"./client/*"
			],
			"server/*": [
				"./server/*"
			]
		}
	},
	"include": [
		"src/common/**/*.ts",
		"src/client/**/*.ts"
	],
	"exclude": [
		".git",
		"node_modules"
	]
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:41 (23 by maintainers)

github_iconTop GitHub Comments

17reactions
alecmevcommented, Jun 25, 2018

Why make a major release so quickly? I do agree that, as @weswigham says, “the build orchestrator is not part of the build it orchestrates”, but the fix is kind of on the extreme side. Why not at least try taking some useful parts out of the tsconfig.json, if there is one? esModuleInterop, lib, typeRoots, exclude, the strictness settings, etc.

Sticking to v6 for now, since I don’t want to:

  • Maintain a separate tsconfig.json just for ts-node.
  • Augment every single ts-node invocation with a --project.
  • Use triple-slash pragmas where I previously didn’t have to.

It’s possible that I’m in the tiny minority here, but the discussion was way too short for this change, IMO.

14reactions
alecmevcommented, Jun 30, 2018

@blakeembrey Thank you for taking time to look into this. Decided to investigate this myself, and wow, still got some misconceptions. Everything was working, so I was sure I had it all figured out…

Indeed, typeRoots was doing nothing. The typings were being picked up by TypeScript’s default include (my tsconfig.json has only an exclude). It would work if the typings did have the right shape, but typeRoots is for global declarations, paths is a better fit for that. Which is conveniently omitted in tsconfig.json docs, and is instead described in module resolutions docs.

The “fix” is to put this in place of typeRoots:

"baseUrl": ".",
"paths": { "*" : ["typings/*"] }

And translate all typings from typings/*.d.ts to typings/*/index.d.ts, i.e. from this:

// typings/some-npm-package.d.ts
declare module "some-npm-package" {
  const x: string;
  export = x;
}

To this:

// typings/some-npm-package/index.d.ts
declare const x: string;
export = x;

I’m choosing this approach because I always wanted my typings to look the same as what you would find on DefinitelyTyped, and now it’s possible, and it works with both ts-node and tsc.

I also have typings like this:

// typings/svg.d.ts
declare module "*.svg" {
  const x: string;
  export default x;
}

These are okay to leave as-is, since they make sense only in the context of a bundler like Webpack, and ts-node shouldn’t be able to execute files which import SVGs and such. One exception is JSON, but there’s a setting for it now in TS 2.9. This way, there’s no need in involving typeRoots at all.

I’m glad this change broke my stuff, now I have a better config and a better understanding of it, thanks.

Edit

Have fiddled around some more, and found out that paths works with declare module 'some-npm-package' too, so if you’re in a rush, you don’t even have to do the translation part, just make sure the directory matches the module.

Adding typings/*/index.d.ts to exclude can help with some typos/mistakes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript getting error TS2304: cannot find name ' require'
I am getting the error "TS2304: Cannot find name 'require' " when I attempt to transpile a simple TypeScript Node.js page. I have...
Read more >
cannot find module 'protractor' or its corresponding type ...
Angular 10 error "Cannot find module '@angular/core' or its corresponding type declarations" ... error TS2304: Cannot find name 'beforeEach'.
Read more >
Changelog - Cypress Documentation
0 regression where using custom reporters would cause Cypress to throw a 'Cannot find module' error. Fixes #24607; Fixed testIsolation configuration validation ...
Read more >
Firebase JavaScript SDK Release Notes - Google
0 upgraded TypeScript only in the root. Fixed a bug that caused Firebase SDKs to throw an error in Firefox browsers when third-party...
Read more >
Upgrading to TensorFlow.js 3.0
When using typescript<4.4 , the following error will occur. node_modules/@webgpu/types/dist/index.d.ts:587:16 - error TS2304: Cannot find name ' ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found