2016-03-02 104 views
1

我使用webpack和babel使用ES6語法創建應用程序。問題是我無法導入express無法使用Babel和Webpack導入Express

(注:我可以導入(和要求)節點模塊 「路徑」,沒有檢查任何更多)

這裏是我的應用程序:

import express from 'express';

甚至低於結果在相同的錯誤:

var app = require('express');

這裏是我的webpack.config.js

module.exports = { 
    entry: './src/app.js', 
    output: { 
     path: 'builds', 
     filename: 'bundle.js' 
    }, 
    module: { 
     loaders: [ 
      { 
       test: /\.js/, 
       loader: 'babel', 
       include: __dirname + '/src', 
       exclude: /node_modules/, 
       query: { 
        presets: ['es2015'] 
       } 
      } 
     ], 
    } 
}; 

我試過下面還有:

exclude: [/node_modules/], 
exclude: __dirname + '/node_modules', 

但我仍然不斷收到其起始於非常大的堆棧跟蹤:

WARNING in ./~/express/lib/view.js 
Critical dependencies: 
78:29-56 the request of a dependency is an expression 
@ ./~/express/lib/view.js 78:29-56 

ERROR in ./~/express/lib/request.js 
Module not found: Error: Cannot resolve module 'net' in /home/projects/node_modules/express/lib 
@ ./~/express/lib/request.js 18:11-25 

ERROR in ./~/express/lib/view.js 
Module not found: Error: Cannot resolve module 'fs' in /home/projects/node_modules/express/lib 
@ ./~/express/lib/view.js 18:9-22 

並與

結束
@ ./~/mime/mime.js 87:12-35 

ERROR in ./~/mime-db/db.json 
Module parse failed: /home/projects/node_modules/mime-db/db.json Line 2: Unexpected token : 
You may need an appropriate loader to handle this file type. 
| { 
| "application/1d-interleaved-parityfec": { 
|  "source": "iana" 
| }, 
@ ./~/mime-db/index.js 11:17-37 

我顧這是因爲node_modules文件夾不被忽略?

而且這是我package.json如果可能模塊的版本是一個問題:

npm install json-loader --save-dev 

而且在webpack.config添加此裝載機:

{ 
    "name": "testing", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
    }, 
    "author": "", 
    "license": "MIT", 
    "dependencies": { 
    "express": "^4.13.4" 
    }, 
    "devDependencies": { 
    "babel-core": "^6.6.0", 
    "babel-loader": "^6.2.4", 
    "babel-preset-es2015": "^6.6.0", 
    "webpack": "^1.12.14" 
    } 
} 
+0

我也有這個問題!獲取大量'無法解析'fs''和'無法解析'網絡' –

回答

1

這可以通過使用JSON-裝載機固定:

{ test: /\.json$/, loader: "json-loader" } 
+0

爲什麼我需要添加另一個加載程序來解決另一個加載程序的問題?爲什麼我不能忽略node_modules? –

+0

我沒有在這個問題上挖得太深,只爲我自己修復。看起來webpack發現mime/db.json並且無法處理這種文件類型。 –

+0

如果你不在webpack config中指定target:'node',你也會遇到像「net」,「fs」等node.js模塊的問題。有些人爲前端和後端創建分離的配置。 –

0

請看這answer。 SudoPlz - 共享工作示例。看起來像Michael Plakhov建議的方法。

+0

我不想像在答案中那樣將快遞包裝到我的代碼中。我只想運行babel transpiler。而已。沒有節點模塊或捆綁或任何東西。 –

+0

如果你只想要傳譯,爲什麼不專門有一個npm腳本來傳譯你的快遞代碼? Webpack是爲前端打包東西的。 – Sgnl

+0

這個答案適用於我..因爲問題說,導入用webpack表示..但它也打包在瀏覽器上的節點代碼 – Alexander

相關問題