prerender.ts(7,62)中的错误:错误TS2307:找不到模块'./dist-prerender/main.bundle'

时间:2019-06-12 08:44:16

标签: angular typescript prerender

虽然我正在编译角度代码以在低于错误时进行预渲染

  

prerender.ts(7,62)中的错误:错误TS2307:找不到模块'./dist-prerender/main.bundle'

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! XXXX@2.0.0 build: `ng build && ng build --project prerender --output-hashing=none`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the XXXXX@2.0.0 build 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!     /root/.npm/_logs/2019-06-12T07_43_21_717Z-debug.log

也许我的应用程序无法生成main.bundle文件

对于预渲染,我在angular.json中添加了一些代码

  

angular.json

"prerender" : {

        "root": "",
        "sourceRoot": "src",
        "projectType": "application",
        "prefix": "app",
        "schematics": {},
        "architect": {
                "build": {
                        "builder": "@angular-devkit/build-angular:server",
                "options": {
                        "outputPath": "dist-prerender",
                        "main": "main.prerender.ts",
                        "tsConfig": "tsconfig.prerender.json"
                },
                        "configurations": {
                        "production": {
                                "fileReplacements": [
                                        {
                                         "replace": "src/environments/environment.ts",
                                         "with": "src/environments/environment.prod.ts"
                                        }
                                ],
                                "optimization": true,
                                "outputHashing": "all",
                                "sourceMap": false,
                                "extractCss": true,
                                "namedChunks": false,
                                "aot": true,
                                "extractLicenses": true,
                                "vendorChunk": false,
                                "buildOptimizer": true
                        }
                }}
                }`
  

tsconfig.prerender.json

  {
  "extends": "./tsconfig.app.json",
  "compilerOptions": {
    "outDir": "../out-tsc/prerender",
    /* node only understands commonjs for now*/
    "module": "commonjs"
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ],
  "angularCompilerOptions": {
    "entryModule": "app/app.prerender.module#AppPrerenderModule"
  }
}

1 个答案:

答案 0 :(得分:0)

在执行命令ng build --project prerender --output-hashing=none的同时,webpack.server.config.jsprerender.ts的这些文件也被执行,这对我来说是一个错误。为了消除此错误,我只需先执行ng build --project prerender --output-hashing=none此命令,然后在编译该命令后添加prerender.ts文件。