简体   繁体   English

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

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

I have a problem when I can use mongoose with webpack. 我可以在webpack中使用猫鼬时遇到问题。 I already installed it as a dependency but when I want to use the mongoose object and execute it it commands me that it can not find the "." 我已经将它安装为依赖项,但是当我想使用猫鼬对象并执行它时,命令我找不到它。 Module. 模块。 And that seems very strange, I've been looking for a good time, I installed the dependencies again, delete the npm cache, reinstall webpack 这似乎很奇怪,我一直在寻找一个好时机,我再次安装了依赖项,删除了npm缓存,重新安装了webpack

I would appreciate your support, thank you very much 非常感谢您的支持,非常感谢

webpack.config.js 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 的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 index.ts

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

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

Error 错误

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 "."

Your mongoose is npm module. 你的猫鼬是npm模块。 TypeScript may not recognized the way you import it. TypeScript可能无法识别您导入它的方式。 try the nodejs require module loading system. 尝试使用nodejs require模块加载系统。

var mongoose  = require('mongoose');

or 要么

const mongoose  = require('mongoose');

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

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