我执行时遇到错误
npm run start (node bin / index.js)
当我使用gulp脚本编译时工作正常没有错误 .
我的 gruntfile.js 是:
const gulp = require('gulp');
const ts = require('gulp-typescript');
const JSON_FILES = ['src/*.json', 'src/**/*.json'];
// pull in the project TypeScript config
var tsProject = ts.createProject('tsconfig.json');
gulp.task('scripts', () => {
const tsResult = tsProject.src()
.pipe(tsProject());
return tsResult.js.pipe(gulp.dest('dist'));
});
gulp.task('watch', ['scripts'], () => {
gulp.watch('src/**/*.ts', ['scripts']);
});
gulp.task('assets', function() {
return gulp.src(JSON_FILES)
.pipe(gulp.dest('bin'));
});
我的 tsconfig.json 是:
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "bin/",
"module": "es2015",
"types": [],
"forceConsistentCasingInFileNames": false,
"lib": ["es2015", "dom"],
"noImplicitAny": false,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": true
},
"include": [
"src/index.ts",
"polyfills.ts"
],
"exclude": [
"src/test.ts",
"**/*.spec.ts",
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
我的index.ts是:
import * as http from 'http';
import * as debug from 'debug';
import App from './App'
//debug('ts-express:server');
const port = normalizePort(process.env.PORT || 8080);
App.set('port', port);
const server = http.createServer(App)
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val: number|string): number|string|boolean {
let port: number = (typeof val === 'string') ? parseInt(val, 10) : val;
if (isNaN(port)) return val;
else if (port >= 0) return port;
else return false;
}
function onError(error: NodeJS.ErrnoException): void {
if (error.syscall !== 'listen') throw error;
let bind = (typeof port === 'string') ? 'Pipe ' + port : 'Port ' + port;
switch(error.code) {
case 'EACCES':
console.error(`${bind} requires elevated privileges`);
process.exit(1);
break;
case 'EADDRINUSE':
console.error(`${bind} is already in use`);
process.exit(1);
break;
default:
throw error;
}
}
function onListening(): void {
let addr = server.address();
let bind = (typeof addr === 'string') ? `pipe ${addr}` : `port ${addr.port}`;
debug(`Listening on ${bind}`);
}
我在控制台 npm run start 中写入时导入的文件不会在require中进行转换 .
The errors are :
npm跑步开始
back-new@1.0.0启动C:\ Users \ XXXX \ back-new node bin / index.js
internal / modules / cjs / loader.js:582 throw err; ^
错误:在模块的Function.Module._load(internal / modules / cjs / loader.js:506:25)的Function.Module._resolveFilename(internal / modules / cjs / loader.js:580:15)中找不到模块'App' .require(internal / modules / cjs / loader.js:636:17)at object(internal / modules / cjs / helpers.js:20:18)at . (C:\ Users \ David M \ source \ repos \ CV_Development \ cv_backend \ nodejs \ back-new \ bin \ index.js:2:11)在Module._compile(internal / modules / cjs / loader.js:688: 30)在try.ModuleLoad(内部/ modules / cjs / loader.js:598:32)的Object.Module._extensions..js(internal / modules / cjs / loader.js:699:10)处 . modules / cjs / loader.js:537:12)在Function.Module._load(internal / modules / cjs / loader.js:529:3)npm ERR!代码ELIFECYCLE npm ERR!错误1 npm ERR! back-new@1.0.0 start: node bin/index.js
npm ERR!退出状态1 npm ERR!错误的ERR!在back-new@1.0.0启动脚本失败 . 错误的ERR!这可能不是npm的问题 . 上面可能有额外的日志记录输出 .
错误的ERR!可以在以下位置找到此运行的完整日志:npm ERR! C:\ Users \用户XXX \应用程序数据\漫游\ NPM-cache_logs \ 2018-12-03T22_34_00_257Z-的debug.log
index.js 是:
var http = require("http");
var App = require("App");
var port = normalizePort(process.env.PORT || 8080);
App.set('port', port);
var server = http.createServer(App);
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
function normalizePort(val) {
var port = (typeof val === 'string') ? parseInt(val, 10) : val;
if (isNaN(port))
return val;
else if (port >= 0)
return port;
else
return false;
}
function onError(error) {
if (error.syscall !== 'listen')
throw error;
var bind = (typeof port === 'string') ? 'Pipe ' + port : 'Port ' + port;
switch (error.code) {
case 'EACCES':
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
}
function onListening() {
var addr = server.address();
var bind = (typeof addr === 'string') ? "pipe " + addr : "port " + addr.port;
debug("Listening on " + bind);
}
//# sourceMappingURL=index.js.map
怎么了?