导入空类时出错,错误TS2307:找不到模块' menu'

时间:2017-10-18 19:45:51

标签: javascript reactjs typescript ecmascript-6

我只想尝试导入一个简单的空白导出类。我不知道为什么它找不到该文件,因为它与导入它的类位于同一目录中。我搜索过谷歌类似的错误代码,但没有解决方案对我有用,而且它是一个相对简单的问题,所以我很困惑。

错误:

error TS2307: Cannot find module 'menu'

文件夹结构:

node_modules/
src/
    entry.tsx
    menu.tsx
index.html
package-lock.json
package.json
tsconfig.json
webpack.config.js

entry.tsx

import menu from 'menu';

menu.tsx

export default class menu { }

webpack.config

var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/js/entry.tsx',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: [ 'css-loader' ]
        })
      },
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin({
            filename: "bundle.css"
        }),
    new HtmlWebpackPlugin({
      title: 'Custom template',
      template: 'index.html'
    })
  ]
};

的package.json

{
  "name": "helloworld",
  "version": "1.0.0",
  "description": "Hello there",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack-dev-server",
    "build:prod": "webpack -p"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.2",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.24.1",
    "css-loader": "^0.28.7",
    "extract-text-webpack-plugin": "^3.0.1",
    "html-webpack-plugin": "^2.30.1",
    "react": "^16.0.0",
    "react-dom": "^16.0.0",
    "style-loader": "^0.19.0",
    "ts-loader": "^2.3.7",
    "typescript": "^2.5.3",
    "webpack": "^3.7.1",
    "webpack-dev-server": "^2.9.1"
  },
  "dependencies": {
    "@types/react": "^16.0.13",
    "react": "^16.0.0",
    "react-dom": "^16.0.0"
  }
}

1 个答案:

答案 0 :(得分:4)

我认为如果你不在你的" include"中包含相对路径,Webpack会尝试加载Node包。线。

尝试在entry.tsx中更改此行:

import menu from './menu';

对此:

import java.util.Scanner; public class DemoPayroll { public static void main(String[] args) { Payroll newEmpInfoObject = new Payroll(); System.out.println("Enter name"); Scanner keyboard = new Scanner(System.in); String name = keyboard.nextLine(); System.out.println("Enter Address"); String address = keyboard.nextLine(); System.out.println("Enter Hourly Pay"); double payrate = keyboard.nextDouble(); System.out.println("Enter Hours Worked"); double hours = keyboard.nextDouble(); newEmpInfoObject.printEmpInfo(); newEmpInfoObject.getGrossPayEarned(); } } public class Payroll { private String name; private String address; private double payrate; private double hours; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getPayrate() { return payrate; } public void setPayrate(double payrate) { this.payrate = payrate; } public double getHours() { return hours; } public void setHours(double hours) { this.hours = hours; } public Object printEmpInfo() { System.out.println(name); System.out.println(address); return address; } }