javascript - ESLint 在基于 es6 的导入时失败 npm run build

标签 javascript node.js ecmascript-6 eslint

我的 server.js 看起来像

import express from 'express';
import open from 'open';
import compression from "compression";

const app = express();

app.get('/', function (req, res) {
  res.send('Hello World!');
});

app.get('/users', function (req, res) {
  // Hard coding for simplicity. Pretend this hits a real database
  res.json([
    {"id": 1, "firstName": "Bob", "lastName": "Smith", "email": "bob@gmail.com"},
    {"id": 2, "firstName": "Tammy", "lastName": "Norton", "email": "tnorton@yahoo.com"},
    {"id": 3, "firstName": "Tina", "lastName": "Lee", "email": "lee.tina@hotmail.com"}
  ]);
});


// Functions to start the server
let startDevServer = function(port) {
  runServer(port);
};

let startProdServer = function(port) {
  app.use(express.static('dist'));
  app.use(compression());

  runServer(port);
};

let runServer = function (port) {
  app.listen(port, function (err) {
    if (err) {
      console.log(err);
    } else {
      open(`http://localhost:${port}`);
    }
  });
};

module.exports = {
  startDevServer: startDevServer,
  startProdServer: startProdServer
};

然后在srcServer.js中看起来像

import {startDevServer} from "./server";
startDevServer(3001);

distServer.js看起来像

import {startProdServer} from "./server";
startProdServer(3001);

当我运行 npm run build 时,我看到了

/Users/harit/org/code/apollo-server/src/index.js (0/1)
  !  9:5  Unexpected console statement  no-console

/Users/harit/org/code/apollo-server/buildScripts/distServer.js (1/0)
  ✖  1:9  startProdServer not found in './server'  import/named

/Users/harit/org/code/apollo-server/buildScripts/server.js (0/1)
  !  36:7  Unexpected console statement  no-console

/Users/harit/org/code/apollo-server/buildScripts/srcServer.js (1/0)
  ✖  1:9  startDevServer not found in './server'  import/named

✖ 2 errors ! 2 warnings (12:48:46 PM)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! apollo-server@1.0.0 lint: `esw webpack.config.* src buildScripts --color`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the apollo-server@1.0.0 lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/harit/.npm/_logs/2017-09-07T00_48_46_543Z-debug.log
ERROR: "lint" exited with 1.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! apollo-server@1.0.0 prebuild: `npm-run-all clean-dist lint`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the apollo-server@1.0.0 prebuild script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

我的 .eslintrc.json 看起来像

{
  "root": true,
  "extends": [
    "eslint:recommended",
    "plugin:import/errors",
    "plugin:import/warnings"
  ],
  "parserOptions": {
    "ecmaVersion": 7,
    "sourceType": "module"
  },
  "env": {
    "browser": true,
    "node": true,
    "mocha": true,
    "es6": true
  },
  "rules": {
    "no-console": 1
  }
}  

package.json看起来像

{
  "name": "apollo-server",
  "version": "1.0.0",
  "description": "Serving Apollo's request",
  "main": "src/index.js",
  "scripts": {
    "prestart": "babel-node buildScripts/startMessage.js",
    "start": "npm-run-all --parallel open:src lint:watch",
    "open:src": "babel-node buildScripts/srcServer.js",
    "lint": "esw webpack.config.* src buildScripts --color",
    "lint:watch": "npm run lint -- --watch",
    "clean-dist": "rimraf ./dist && mkdir dist",
    "prebuild": "npm-run-all clean-dist lint",
    "build": "babel-node buildScripts/build.js",
    "postbuild": "babel-node buildScripts/distServer.js"
  },
  "author": "Harit Himanshu",
  "dependencies": {
    "whatwg-fetch": "^2.0.3"
  },
  "devDependencies": {
    "babel-cli": "6.16.0",
    "babel-core": "6.17.0",
    "babel-loader": "6.2.5",
    "babel-preset-latest": "6.16.0",
    "babel-register": "6.16.3",
    "chai": "3.5.0",
    "chalk": "1.1.3",
    "cheerio": "0.22.0",
    "compression": "1.6.2",
    "cross-env": "3.1.3",
    "css-loader": "0.25.0",
    "eslint": "3.8.1",
    "eslint-plugin-import": "2.0.1",
    "eslint-watch": "2.1.14",
    "express": "4.14.0",
    "extract-text-webpack-plugin": "1.0.1",
    "html-webpack-plugin": "2.22.0",
    "jsdom": "9.8.0",
    "json-loader": "^0.5.7",
    "json-schema-faker": "0.3.6",
    "json-server": "0.8.22",
    "localtunnel": "1.8.1",
    "mocha": "3.1.2",
    "nock": "8.1.0",
    "npm-run-all": "3.1.1",
    "nsp": "2.6.2",
    "numeral": "1.5.3",
    "open": "0.0.5",
    "rimraf": "2.5.4",
    "style-loader": "0.13.1",
    "surge": "0.18.0",
    "webpack": "1.13.2",
    "webpack-dev-middleware": "1.8.4",
    "webpack-hot-middleware": "2.13.0",
    "webpack-md5-hash": "0.0.5"
  }
}

这里有什么问题?我该如何解决?谢谢

最佳答案

您不应将 importmodule.exports 结合使用。

let startDevServer = function(port) {
  runServer(port);
};

let startProdServer = function(port) {
  app.use(express.static('dist'));
  app.use(compression());

  runServer(port);
};

module.exports = {
  startDevServer: startDevServer,
  startProdServer: startProdServer
};

应转换为使用 ES6 export 语法:

export let startDevServer = function(port) {
  runServer(port);
};

export let startProdServer = function(port) {
  app.use(express.static('dist'));
  app.use(compression());

  runServer(port);
};

关于javascript - ESLint 在基于 es6 的导入时失败 npm run build,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46086265/

相关文章:

node.js - AWS Lambda : How to store secret to external API?

javascript - ES6 - 将类原型(prototype)附加到单例

Javascript 推送数组高级

javascript - 如何防止 Intellij 再次破坏代码

javascript - 动态更改同一类失败的所有html元素的背景颜色

javascript - 使用 Jest 在另一个函数中测试一个函数

javascript - Chrome 扩展 : Extension context invalidated when getting URL

node.js - Node JS - 渗透表单.submit

reactjs - 如何从 React JS 中的 Promise 对象检索数据

javascript - React ES6 导入无状态组件