繁体   English   中英

错误:找不到带有Webpack和Typescript的模块“。” mongodb

[英]Error: Cannot find module “.” mongodb with webpack and typescript

我可以在webpack中使用猫鼬时遇到问题。 我已经将它安装为依赖项,但是当我想使用猫鼬对象并执行它时,命令我找不到它。 模块。 这似乎很奇怪,我一直在寻找一个好时机,我再次安装了依赖项,删除了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 "."

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

var mongoose  = require('mongoose');

要么

const mongoose  = require('mongoose');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM