简体   繁体   English

无法通过Babel和Webpack导入Express

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

I'm using webpack and babel to create an app using ES6 syntax. 我正在使用webpack和babel使用ES6语法创建一个应用程序。 The problem is that I'm not able to import express . 问题是我无法导入express

(Note: I'm able to import (and require) node module "path", didn't check any more) (注意:我能够导入(并要求)节点模块“路径”,不再检查了)

Here's my app: 这是我的应用程序:

import express from 'express';

Even the below results in same error: 即使是以下情况也会导致相同的错误:

var app = require('express');

Here's my webpack.config.js 这是我的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']
                }
            }
        ],
    }
};

I've tried the following as well: 我也尝试了以下方法:

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

But I still keep getting a very big stack trace which starts with: 但是我仍然不断获得大量的堆栈跟踪信息,其开始于:

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

And ends with 并以

 @ ./~/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

I guess this is because node_modules folder is not being ignored? 我猜这是因为node_modules文件夹没有被忽略吗?

Also this is my package.json if maybe module versions are a problem: 如果模块版本有问题,这也是我的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"
  }
}

This can be fixed by using json-loader: 这可以通过使用json-loader来解决:

npm install json-loader --save-dev

And in webpack.config add this loader: 然后在webpack.config中添加以下加载器:

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

Please, look at this answer . 请看看这个答案 SudoPlz - shared working example. SudoPlz-共享的工作示例。 Looks like the method Michael Plakhov suggested. 看起来像Michael Plakhov建议的方法。

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

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