Egg: ts项目直接跑npm start报config.keys不存在

Created on 30 May 2018  ·  11Comments  ·  Source: eggjs/egg

  • Node Version: v9.3.0
  • Egg Version: 2.6.1
  • Plugin Name: egg
  • Plugin Version:2.6.1
  • Platform:macOS
  • Mini Showcase Repository: 按照官方生成的案例
    启用了npm run ci之后再启动就不会报错了

    2018-05-30 20:51:39,371 ERROR 21665 [-/127.0.0.1/-/4ms GET /] nodejs.Error: Please set config.keys first
    at Application.get keys [as keys] (/Users/tusm/github/mygithub/work/node_modules/egg/lib/application.js:318:15)
    at Object.get cookies [as cookies] (/Users/tusm/github/mygithub/work/node_modules/egg/app/extend/context.js:18:66)
    at Object.get [egg-security#CSRF_SECRET] (/Users/tusm/github/mygithub/work/node_modules/egg-security/app/extend/context.js:78:33)
    at Object.ensureCsrfSecret (/Users/tusm/github/mygithub/work/node_modules/egg-security/app/extend/context.js:89:13)
    at csrf (/Users/tusm/github/mygithub/work/node_modules/egg-security/lib/middlewares/csrf.js:14:9)
    at dispatch (/Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:42:32)
    at /Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:34:12
    at dispatch (/Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:42:32)
    at session (/Users/tusm/github/mygithub/work/node_modules/koa-session/index.js:41:13)
    at dispatch (/Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:42:32)

Most helpful comment

npm start 一般是用于在生产中跑的,而生产代码是需要将 ts 编译成 js,只有在开发的时候,才会使用 ts-node 来跑应用,所以请使用 npm dev

如果要在生产中跑 npm start,请先 tsc 编译一下。

原因是,在生产如果跑 ts-node,会产生无谓的编译开销。

All 11 comments

Translation of this issue:


ts project runs npm start directly config.keys does not exist

  • Node Version: v9.3.0
  • Egg Version: 2.6.1
  • Plugin Name: egg
  • Plugin Version: 2.6.1
  • Platform:macOS
  • Mini Showcase Repository: According to the official generated case
    After starting npm run ci will not start again

    2018-05-30 20:51:39,371 ERROR 21665 [-/127.0.0.1/-/4ms GET /] nodejs.Error: Please set config.keys first
        At Application.get keys [as keys] (/Users/tusm/github/mygithub/work/node_modules/egg/lib/application.js:318:15)
        At Object.get cookies [as cookies] (/Users/tusm/github/mygithub/work/node_modules/egg/app/extend/context.js:18:66)
        At Object.get [egg-security#CSRF_SECRET] (/Users/tusm/github/mygithub/work/node_modules/egg-security/app/extend/context.js:78:33)
        At Object.ensureCsrfSecret (/Users/tusm/github/mygithub/work/node_modules/egg-security/app/extend/context.js:89:13)
        At csrf (/Users/tusm/github/mygithub/work/node_modules/egg-security/lib/middlewares/csrf.js:14:9)
        At dispatch (/Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:42:32)
        At /Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:34:12
        At dispatch (/Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:42:32)
        At session (/Users/tusm/github/mygithub/work/node_modules/koa-session/index.js:41:13)
        At dispatch (/Users/tusm/github/mygithub/work/node_modules/koa-compose/index.js:42:32)

npm start 一般是用于在生产中跑的,而生产代码是需要将 ts 编译成 js,只有在开发的时候,才会使用 ts-node 来跑应用,所以请使用 npm dev

如果要在生产中跑 npm start,请先 tsc 编译一下。

原因是,在生产如果跑 ts-node,会产生无谓的编译开销。

keys肯定是有配置的,不然npm run ci生成后也会报错的

楼上已经说很清楚了, npm start 的前提条件是 tsc 编译,在文档里面有提到的。

‘‘ 正式环境下,我们更倾向于把 ts 构建为 js ,建议在 ci 上构建并打包。’’是这句话吗?还是有其他文档提到?
我理解的是可以直接用ts运行,只是推荐使用js,
现在ts直接执行报错,不是开销问题,编译后就不会有问题?难道这不是一个问题吗

所谓 keys 不存在的原因,就是因为线上 egg 的 loader 根本不会去加载 ts 文件。

线下能加载 ts 是 egg-bin 提供的,ts-node 这个能力只是在 egg-bin 开发期提供的,egg-scripts 不提供。
如果你期望在线上直接运行 ts,那就需要自行引入下。

你这么说我就明白了,官方问题没有强制说明,我以为只是推荐

@chenyulun 这个我们会在文档里完善一下描述,现在确实会容易让人误解

我也是报这个错误,而且都是编译好了ts文件,为什么还要报这个错误

@isLishude 要确认下你是否是在编译后的目录下执行的,且编译后的目录下存在 config/config.default.js ,且这个文件是符合 egg 规范的

不好意思我的失误,dockerignore 把 config 文件夹包含进去了。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xcstream picture xcstream  ·  3Comments

wujianling picture wujianling  ·  3Comments

lvgg3271 picture lvgg3271  ·  3Comments

dizhifeng picture dizhifeng  ·  3Comments

Ailein picture Ailein  ·  3Comments