Typescript import index.ts is not a module

11,210

Some ideas are:

  • reducers is a folder and tsc is trying to search for ./reducers/index.ts
  • reducers is a file and is not readable by tsc
  • recuders is a valid file but has a different export system. check UMD, systemjs, commonjs includes

You can include them via: import counterReducer = require('./recuders'); import * as counterReducer from './recuders';

If you want to include a single module via import { mod } from 'file'; Be sure that you export this function, class or whatever you want in reducers.ts

I hope this ideas helped. let me know!

Can you please post your tsconfig.json to ensure everything is correctly setup?

Share:
11,210
MonteCristo
Author by

MonteCristo

Updated on June 04, 2022

Comments

  • MonteCristo
    MonteCristo almost 2 years

    I am trying to import index.ts in a subfolder which has other imports. But I keep getting a typescript error.

    full repo: https://github.com/Shavindra/webpack-react-sw

    (5,32): error TS2306: File 'C:/../src/reducers/index.ts' is not a module.

    I am not quite sure what I am doing wrong here. I am using TS 2.4.1. I've tried restarting the computer/VSCode but nothing seems to work :-|

    // ./src/reducers/counter.reducer.ts    
    export const counterReducer = (state = 0, action) => {
      switch (action.type) {
        case 'INCREMENT':
          return state + 1;
        case 'DECREMENT':
          return state - 1;
        default:
          return state;
      }
    };
    
    
    // ./src/reducers/index.ts
    export * from './counter.reducer';
    
    
    // ./src/app.ts
    import * as React from 'react';
    import * as React from 'react';
    import * as ReactDOM from 'react-dom';
    import { createStore } from 'redux';
    import { Counter } from './components/counter/counter.component';
    import { counterReducer } from './reducers';
    
    
    const store = createStore(counterReducer);
    const rootEl = document.getElementById('root');
    
    const render = () => ReactDOM.render(
        <Counter
            value={store.getState()}
            onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
            onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
        />,
        rootEl
    );
    
    render();
    store.subscribe(render);
    
    // tsconfig.json
    
    {
      "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "sourceMap": true,
        "jsx":"react",
        "lib": [
          "webworker",
          "es6",
          "scripthost",
          "dom"
        ]
      },
      "files": [ "node_modules/@types/react-dom/index.d.ts", "node_modules/@types/react/index.d.ts", "typings/file-loader.d.ts"  ],
      "exclude": [
        "typings/browser.d.ts",
        "typings/browser",
        "node_modules"
      ]
    }
    
  • MonteCristo
    MonteCristo almost 7 years
    I've added the tsconfig
  • wartoshika
    wartoshika almost 7 years
    I tried to setup your files at my workspace and i found no errors within the .ts files. After i removed the files array from the tsconfig, and i tried to tsc the app.ts everything works fine. May you need to add a rootDir at the tsconfig to ensure the tsc find all files?
  • MonteCristo
    MonteCristo almost 7 years
    github.com/Shavindra/webpack-react-sw here is my repo if it helps
  • wartoshika
    wartoshika almost 7 years
    I cloned your repo and i figured out that your react component works just fine. I think the problem could be solved when first: change the tsconfig.json to use the ts@^2.x syntax like tsconfig.json. Second: The service worker is not correctly setup. this should not be bundled with the file-loader plugin. this causes the error. may you find another solutition or ask if it itsn't working. Hope this helped!
  • TechNyquist
    TechNyquist about 4 years
    Solving tip for me was to change the "export system" in tsconfig.json, it was system for supporting single file compilation; moved it to CommonJS and solved. Apparently this is relevant when working with index.ts batch import technique.