首页 > 解决方案 > 当我执行 npm run start 时出现错误

问题描述

执行时出现错误

npm 运行开始(节点 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中写入时,导入文件不会在要求中转换。

错误是

npm 运行开始

back-new@1.0.0 开始 C:\Users\XXXX\back-new 节点 bin/index.js

内部/模块/cjs/loader.js:582 抛出错误;^

错误:在 Function.Module._load (internal/modules/cjs/loader.js:506:25) 的 Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15) 处找不到模块“App”在 Module.require (internal/modules/cjs/loader.js:636:17) 在 require (internal/modules/cjs/helpers.js:20:18) 在 Object. (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) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) 在 Module.load (internal/modules/cjs/loader.js:598:32) 在 tryModuleLoad (internal/ modules/cjs/loader.js:537:12) 在 Function.Module._load (internal/modules/cjs/loader.js:529:3) npm ERR!代码 ELIFECYCLE npm 错误!errno 1 npm 错误!back-new@1.0.0 开始:node bin/index.js npm 错误!退出状态 1 npm ERR!npm 错误!在 back-new@1.0.0 启动脚本中失败。npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。

npm 错误!可以在以下位置找到此运行的完整日志:npm ERR!C:\Users\XXX\AppData\Roaming\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

怎么了?

标签: node.jsapiexpress

解决方案


推荐阅读