如何在没有错误的情况下编译这个Typescript Webpack项目?

时间:2015-11-28 08:28:06

标签: node.js npm typescript webpack definitelytyped

我正在尝试将两个.js文件转换为我工作的webpack node.js项目中的.ts文件并编译它(actions.ts和flux.ts)。运行时

webpack --progress --colors

我收到此错误:

ERROR in ./src/app/tools/flux.ts
(2,11): error TS2304: Cannot find name 'require'.

ERROR in ./src/app/tools/flux.ts
(4,1): error TS2304: Cannot find name 'module'.

ERROR in ./src/app/actions/actions.ts
(2,12): error TS2304: Cannot find name 'require'.

ERROR in ./src/app/actions/actions.ts
(11,1): error TS2304: Cannot find name 'module'.

如何解决这些问题?

您可以在此屏幕截图中看到我的文件夹结构以及我正在尝试编译的代码:

Folder structure

如果有帮助的话,这是我的webpack配置:

'use strict';

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var path = require('path');
var rootPath = __dirname; //site
var srcPath = path.join(rootPath, 'src'); //site/src

module.exports =
{
    bail: true,
    cache: true,
    context: rootPath,
    debug: true,
    devtool: 'inline-source-map', //'eval-cheap-module-source-map','inline-source-map'
    target: 'web',
    devServer:
    {
        contentBase: './dist',
        historyApiFallback: true
    },
    entry:
    {
        app: path.join(srcPath, 'app/actions/actions.ts'),  //main.jsx
        lib: ['react', 'react-router']
    },
    output:
    {
        path: path.join(rootPath, 'dist'),
        publicPath: '',
        filename: '[name].js',
        library: ['[name]', '[name]'],
        pathInfo: true
    },
    resolve:
    {
        root: srcPath,
        extensions: ['', '.js', '.jsx', '.ts', '.tsx'],
        modulesDirectories: ['node_modules', 'src', 'typings']
    },
    module:
    {
        loaders:
        [
            {test: /\.js?$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules|bower_components)/ },
            {test: /\.jsx?$/, loader: 'babel-loader?cacheDirectory', exclude: /(node_modules|bower_components)/ },
            {test: /\.ts?$/, loader: 'ts-loader?cacheDirectory', exclude: /(node_modules|bower_components)/ },
            {test: /\.tsx?$/, loader: 'ts-loader?cacheDirectory', exclude: /(node_modules|bower_components)/ },
            {test: /\.scss?$/, loaders: ['style', 'css', 'sass']},
            {test: /\.png?$/, loader: 'file-loader'},
            {test: /\.jpg?$/, loader: 'file-loader'},
            {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml'},
            {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},
            {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/font-woff"},
            {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader?mimetype=application/octet-stream"},
            {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file-loader"},
        ]
    },
    plugins:
    [
        new CopyWebpackPlugin
        ([
            { from: 'src/images', to: 'images' }
        ]),
        new webpack.optimize.CommonsChunkPlugin('lib', 'lib.js'),
        new HtmlWebpackPlugin
        ({
            inject: true,
            template: 'src/index.html'
        }),
        new webpack.NoErrorsPlugin()
    ]
};

2 个答案:

答案 0 :(得分:1)

错误基本上是TypeScript抱怨它将JavaScript作为CommonJS模块(requiremodule),但它期待importexport =。磁盘上的源是正确的,所以某些似乎正在转换该代码,然后再转向TypeScript。

我认为问题是TypeScript实际上正在运行两次。如果你看看你的加载器,你会在每个扩展正则表达式的末尾有问号(?)。这使得每个扩展的最后一个字母都是可选的,这是不正确的。在这种情况下,.ts扩展名将与/\.ts?$//\.tsx?$/匹配,因此webpack会对同一文件执行两次TypeScript。第一遍工作正常,但第二遍失败,因为它已经被转换。

您应该从几乎所有的装载机测试中删除问号。问号最好的地方是tsxjsx。因此,为/\.tsx?$/配置的单个加载器将正确匹配.ts.tsx。同样适用于jsx

答案 1 :(得分:0)

@Richard,也许@Peter Varga answer可以帮助你:

  

而不是var mongoose = require('mongoose')尝试以下

     

从'mongoose'导入猫鼬; //或

     

import mongoose = require('mongoose');