错误:找不到模块“。” mongodb与webpack和打字稿

时间:2017-04-13 00:36:59

标签: node.js typescript mongoose webpack

当我可以使用mongoose和webpack时,我遇到了问题。我已经将它安装为依赖项,但是当我想使用mongoose对象并执行它时,它命令我找不到“。”模块。这看起来很奇怪,我一直在寻找好时光,我再次安装了依赖项,删除了npm缓存,重新安装webpack

非常感谢您的支持,非常感谢

webpack.config.js

var path = require("path");

var typescriptLoader = {
    test: /\.ts$/,
    loader: 'ts-loader'
};

module.exports = {
    entry: "./src/main.ts",
    target: "node",
    output: {
        path: path.resolve(__dirname, "dist"),
        publicPath: "dist/",
        filename: "main.js"
    },
    node: {
        __filename: true,
        __dirname: true
    },
    watch: true,
    devServer: {
        contentBase: path.join(__dirname, "public_html"),
        watchContentBase: true
    },
    module: {
        rules: [ typescriptLoader ]
    },
    resolve: {
        extensions: [ '.js', '.ts' ]
    },
    externals: {

    }
}

的package.json

{
  "name": "",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "watch": "webpack --config webpack.config.js -p",
    "start": "node dist/main.js"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "karma": "^1.5.0",
    "ts-loader": "^2.0.1",
    "typescript": "^2.2.1",
    "webpack": "^2.2.1"
  },
  "dependencies": {
    "@types/core-js": "^0.9.39",
    "@types/helmet": "0.0.34",
    "@types/mongoose": "^4.7.10",
    "@types/node": "^7.0.11",
    "body-parser": "^1.17.1",
    "cheerio": "^0.22.0",
    "express": "^4.15.2",
    "helmet": "^3.5.0",
    "mongoose": "^4.9.4",
  }
}

index.ts

import * as mongoose     from 'mongoose'; // ALL OK

console.log(mongoose); // Error when is used

错误

var n;n="undefined"==typeof window?!function(){var e=new Error('Cannot find module "."');throw e.code="MODULE_NOT_FOUND",e}():r(204),/*!
                                                                                             ^
Error: Cannot find module "."

1 个答案:

答案 0 :(得分:0)

你的猫鼬是npm模块。 TypeScript可能无法识别您导入它的方式。尝试nodejs require模块加载系统。

var mongoose  = require('mongoose');

const mongoose  = require('mongoose');