2016-03-02 101 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

相关问题