请勿将index.ejs文件包含在dist文件夹中。

3

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    "sourceMap": true,                     /* Generates corresponding '.map' file. */
    "outDir": "./dist",                        /* Redirect output structure to the directory. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "moduleResolution": "node",            /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
    "esModuleInterop": true,                  /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
    "skipLibCheck": true,                     /* Skip type checking of declaration files. */
    "forceConsistentCasingInFileNames": true  /* Disallow inconsistently-cased references to the same file. */
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules"]
}

错误

{"message":"在目录“/var/www/tutor/dist/views”中查找视图“index”失败","code":50000}

命令

tsc && node dist/app.js -> 构建 dist 文件夹

src > views > index.ejs

不要将 index.ejs 文件包含在 dist 文件夹中。


你能给我们展示一下你的 tsconfig.json 文件吗? - eol
@eol 是的,上传这个。 - gureuso
2
你忘记提问了,但如果你想知道为什么tsc不会将.ejs文件复制到dist文件夹中:你必须手动完成这个操作。TypeScript只处理它自己的源文件,而不是资源文件。 - Guy Incognito
2个回答

1
TypeScript编译器不会复制项目运行所需的其他类型文件,例如EJS视图模板。为了实现这一点,需要创建一个构建脚本,将所有其他文件复制到dist文件夹中。
npm install --save-dev ts-node shelljs @types/shelljs

在项目的根目录下创建一个名为scripts的新文件夹。在tools文件夹中创建一个名为copy-assets.ts的文件。将以下代码复制到此文件中。
import * as shell from "shelljs";

// Copy all the view templates
shell.cp( "-R", "src/views", "dist/" );

更新package.json中的脚本
 "scripts": {
    "prebuild": "ts-node scripts/copy-assets",
    // ..rest
  }

0

我遇到了同样的问题。 将dir设置为“。”即可解决问题。

对我来说,“/views”文件夹在“/src”文件夹中。

app.set("views", "./src/views");
app.set('view engine', 'ejs');

问题重复:https://dev59.com/Cqzla4cB1Zd3GeqPBLv_#51231280


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接