我使用webpack和babel使用ES6语法创建应用程序。问题是我无法导入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
:
{
"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 :(得分:0)
这可以通过使用json-loader来修复:
npm install json-loader --save-dev
在webpack.config中添加此加载器:
{ test: /\.json$/, loader: "json-loader" }
答案 1 :(得分:0)
请看这个answer。 SudoPlz - 共享工作示例。看起来迈克尔普拉霍夫建议的方法。