Search code examples
visual-studio-codetsconfig

VSCODE checkJs not finding modules


I have a very simple react material-ui project.

I recently added a jsconfig.json to the top level folder

{
    "compilerOptions": {
        "target": "es6",
        "checkJs": true,
        "jsx": "react"
    },
    "exclude": [
        "node_modules",
        "data",
        "docs",
        ".cache",
        "dist"
    ]
}

Which is working fine. But there are a few errors that VSCode is finding that I would like to remove:

Cannot find module '@material-ui/core/Button'.

Cannot find module '@material-ui/core/styles'.

Cannot find module 'easy-peasy'.

The imports are working fine, but I rather not just disable ts-check. All these imports are in the ./node-modules tree (including easy-peasy).

(BTW, the code is all JavaScript and not TS).

import { action, createStore, StoreProvider, useStore, useActions, thunk } from 'easy-peasy';
import Button from '@material-ui/core/Button';
import { withStyles } from '@material-ui/core/styles';
import Grid from '@material-ui/core/Grid';
import TextField from '@material-ui/core/TextField';

Solution

  • You probably need to set "module": "commonjs"

    Here's how I'm doing it:

    jsconfig.json

    {
      "compilerOptions": {
          "baseUrl": ".",
          "target": "es6",
          "module": "commonjs",
          "paths": {
            "@components/*": ["./src/components/*"],
            "@constants/*": ["./src/constants/*"],
            "@helpers/*": ["./src/helpers/*"]
          }
      },
      "include": [
        "src/**/*"
      ]
    

    See the answer I got on my issue on Github Vscode

    Yes if you are importing modules from node_modules, you generally need to set "module": "commonjs" ("moduleResolution": "node" should also work I believe). commonjs is the default setting, but "target": "es6" overrides it to use "module": "es6" unless you explicitly specify another "module" setting

    The module options are covered in a bit more detail here: Understanding "target" and "module" in tsconfig