繁体   English   中英

无法通过Babel和Webpack导入Express

[英]Not able to import Express with Babel and Webpack

我正在使用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"
  }
}

这可以通过使用json-loader来解决:

npm install json-loader --save-dev

然后在webpack.config中添加以下加载器:

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

请看看这个答案 SudoPlz-共享的工作示例。 看起来像Michael Plakhov建议的方法。

暂无
暂无

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

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