我的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"
}
}
这是什么问题?我该怎么办呢?感谢
答案 0 :(得分:3)
您不应将import
与module.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);
};