javascript - Typescript import index.ts is not a module - Stack Overflow

admin2025-04-09  0

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

full repo:

(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 puter/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 './ponents/counter/counterponent';
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

{
  "pilerOptions": {
    "module": "monjs",
    "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"
  ]
}

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./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 puter/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 './ponents/counter/counter.ponent';
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

{
  "pilerOptions": {
    "module": "monjs",
    "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"
  ]
}
Share Improve this question edited Jul 10, 2017 at 8:10 MonteCristo asked Jul 6, 2017 at 7:42 MonteCristoMonteCristo 1,5502 gold badges21 silver badges44 bronze badges 2
  • i do not see where you have import .... from 'index'; Maybe possible duplicate of stackoverflow./questions/32805559/… – Digvijay Commented Jul 6, 2017 at 8:01
  • @Digvijay it's in the app.ts? – MonteCristo Commented Jul 6, 2017 at 23:28
Add a ment  | 

1 Answer 1

Reset to default 2

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, monjs 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?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744202091a235846.html

最新回复(0)