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

"Cannot find module" when requiring node_module with package.json types from .js but works fine in .ts #768

Open
ZeikJT opened this issue Feb 12, 2021 · 1 comment

Comments

@ZeikJT
Copy link

ZeikJT commented Feb 12, 2021

I have some npm dependencies like socket.io or cheerio that provide type info for TS via the package.json types field.

This works just fine in a TS file such as server.ts:

import socketIO = require('socket.io')
socketIO.blah // correctly warns about property not existing
socketIO.Socket // correctly knows this exists

In javascript files however I get en error on the require line, ex in server.js:

// @ts-check
const socketIO = require('socket.io') // "Cannot find module 'socket.io' or its corresponding type declarations"

I did try and add a reference path comment/hint but that didn't work in server.js:

// @ts-check
/// <reference path='./node_modules/socket.io/dist/index.d.ts' />
const socketIO = require('socket.io') // "Cannot find module 'socket.io' or its corresponding type declarations"

However, type info comes through just fine for anything I've had to use DefinitelyTyped for. So for example if I have express as a dependency and @types/express as a devDependency then this works in server.js:

// @ts-check
const express = require('express')
express.blah // correctly warns about property not existing
express.static // correctly knows this exists

Seems like something is missing here when it comes to reading the types field off of package.json for dependencies. Is this just broken/missing functionality for javascript files or am I just missing some kind configuration or something?

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

No branches or pull requests

2 participants
@ZeikJT and others