将Typescript应用程序发布到Azure时找不到模块'../lib/tsc.js' [英] Cannot find module '../lib/tsc.js' when releasing typescript application to azure

查看:146
本文介绍了将Typescript应用程序发布到Azure时找不到模块'../lib/tsc.js'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将打字稿应用程序发布到在Linux上运行的Azure应用服务.我正在使用Azure DevOps进行此操作.我已经建立了成功的构建和发布管道,但是当我访问应用程序服务的URL时,会看到一条消息::c Application Error",它表示诊断日志.这些日志给我以下错误:

I'm trying to release a typescript application to a Azure App Service running on Linux. I'm doing this using Azure DevOps. I've setup a build and release pipeline which succeed, but when I visit the URL of my app service, I see a message saying ":c Application Error" and it refers to the diagnostic logs. These logs give me the following error:

错误:找不到模块'../lib/tsc.js'

Error: Cannot find module '../lib/tsc.js'

完整错误日志

myApp@1.0.0 prod /home/site/wwwroot
> npm run build && npm run start
> myApp@1.0.0 build /home/site/wwwroot
> tsc
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module '../lib/tsc.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object. (/home/site/wwwroot/node_modules/.bin/tsc:2:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! myApp@1.0.0 build: `tsc`
npm ERR! Exit status 1

我的Azure Devops环境由构建管道组成,该管道执行npm安装并将其发布到工件和发布管道,该管道执行部署,然后运行启动命令.这些步骤成功完成,并且运行了start命令,但是会产生上述错误.

My Azure Devops environment cosists of build pipeline which performs a npm install and publishes that to an artifact and a release pipeline which performs a deploy and then runs a start command. These steps succeed and the start command is ran, but it creates the above errors.

在创建工件之前,我曾尝试构建应用程序,但这在构建步骤中出现了错误,即找不到导入的文件.我可以运行 npm run prod 命令,而不会在本地出现任何问题.

I've tried building the application before creating the artifact, but this gave errors in the build step where it couldn't find files that were being imported. I can run the npm run prod command without any issues localy.

有人知道这是什么原因吗?

Does anyone have any idea what could be causing this?

Package.json

{
"name": "MyApp",
"version": "1.0.0",
"description": "",
"main": "./dist/index.js",
"scripts": {
"build": "tsc",
"dev": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec ts-node src/index.ts",
"start": "nodemon ./dist/index.js",
"prod": "npm run build && npm run start",
"test": "mocha --ui tdd -r ts-node/register tests/**/*.test.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
    "@google/chatbase": "^1.1.2",
    "@types/express": "^4.17.0",
    "actions-on-google": "^2.7.1",
    "body-parser": "^1.19.0",
    "dotenv": "^8.0.0",
    "express": "^4.17.1",
    "morgan": "^1.9.1",
    "nodemon": "^1.19.1",
    "ssml-builder": "^0.4.3",
    "ts-sinon": "^1.0.17",
    "ts-node": "^8.3.0",
    "tsc-watch": "^2.2.1",
    "typescript": "^3.5.3",
    "uuid": "^3.3.2"
    },
"devDependencies": {
    "@types/chai": "^4.1.7",
    "@types/mocha": "^5.2.7",
    "chai": "^4.2.0",
    "mocha": "^6.1.4"
}
}

Tsconfig.json

{
"compilerOptions": {
"module": "commonjs",
  "moduleResolution": "node",
  "pretty": true,
  "sourceMap": true,
  "target": "es6",
  "outDir": "./dist",
  "baseUrl": "./src",
  "resolveJsonModule": true,
  "esModuleInterop": true,
},
"include": [
  "src/**/*.ts",
  "src/data/*.json",
  "src/data/sounds/*.mp3"
],
"exclude": [
  "node_modules"
]
}

推荐答案

您正在使用 tsc 命令来编译您的打字稿文件,因此,该文件不会在程序包中包含必要的程序包代码结果文件(js),因此您也需要将 node_modules 文件夹(包括软件包文件)上传/发布到/home/site/wwwroot (可以发布 node_modules 和结果文件一起发布).

You are using tsc command to compile your typescript file, for this way, it won't include necessary packages' code to the result file (js), so you need to upload/publish node_modules folder (include package files) to /home/site/wwwroot too (can publish node_modules with result file through release together).

您可以通过webpack编译您的打字稿文件,该文件可以将包的代码包含到结果文件中. https://webpack.js.org/guides/typescript/

You can compile your typescript file through webpack, which can include packages' code to the result file. https://webpack.js.org/guides/typescript/

这篇关于将Typescript应用程序发布到Azure时找不到模块'../lib/tsc.js'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆