2017-04-13 22 views
0

webpackでmongooseを使用できるときに問題があります。私はすでにそれを依存関係としてインストールしましたが、私がmongooseオブジェクトを使用して実行するときに、それを見つけることができないと私に指示します。モジュール。そして、それは非常に奇妙なようだが、私は良い時間を探してきた、私は再び依存関係をインストールし、NPMのキャッシュを削除し、WebPACKのエラー:モジュールが見つかりません " mongodbとwebpackとtypescript

私はあなたのサポートを感謝し

は、

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

答えて

0

あなたのマングースはNPMモジュールです。 TypeScriptは、インポート方法を認識しない可能性があります。 nodejsにモジュールロードシステムが必要です。

var mongoose = require('mongoose'); 

または

const mongoose = require('mongoose'); 
+0

いいえ、それは問題ではありません。何が起こるのは、Webpackパッケージが何らかの理由ですべての依存関係をパッケージ化したときに、mongooseを含んでおらず、** "モジュールをロードできませんでした:mongoose" **の代わりに** "モジュールをロードできませんでした: "**非常に奇妙です –

関連する問題