简体   繁体   English

将 typescript 应用程序发布到 azure 时找不到模块 '../lib/tsc.js'

[英]Cannot find module '../lib/tsc.js' when releasing typescript application to azure

I'm trying to release a typescript application to a Azure App Service running on Linux. I'm doing this using Azure DevOps.我正在尝试将 typescript 应用程序发布到在 Linux 上运行的 Azure 应用服务。我正在使用 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.我已经设置了一个成功的构建和发布管道,但是当我访问我的应用程序服务的 URL 时,我看到一条消息说“:c 应用程序错误”,它指的是诊断日志。 These logs give me the following error:这些日志给我以下错误:

Error: Cannot find module '../lib/tsc.js'错误:找不到模块“../lib/tsc.js”

Full Error Log完整的错误日志

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

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.我的 Azure Devops 环境由构建管道组成,它执行 npm 安装并将其发布到工件和发布管道,它执行部署然后运行启动命令。 These steps succeed and the start command is ran, but it creates the above errors.这些步骤成功并运行了启动命令,但它会产生上述错误。

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.我可以在本地运行 npm run prod命令而不会出现任何问题。

Does anyone have any idea what could be causing this?有谁知道是什么原因造成的?

Package.json 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 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"
]
}

Faced the same issue due to copying node_modules .由于复制node_modules面临同样的问题。

SOLUTION解决方案

The following change in package.json solves the issue: package.json中的以下更改解决了该问题:

"scripts": {
  "build": "./node_modules/typescript/bin/tsc", // not just "tsc"
  ...
}

DESCRIPTION描述

There is an original tsc script placed at node_modules/typescript/bin/tsc , but there is a symlink to this file placed at node_modules/.bin/tsc .node_modules/typescript/bin/tsc中有一个原始的 tsc 脚本,但是在node_modules/.bin/tsc中有一个指向该文件的符号链接。

While the node_modules/.bin/tsc is symlink, importing ../lib/tsc.js works properly, but after copying this is just a file with same content, so the copying is breaking this importing by relative path.虽然node_modules/.bin/tsc是符号链接,但导入../lib/tsc.js可以正常工作,但复制后这只是一个具有相同内容的文件,因此复制会破坏相对路径的导入。

So specifying the path to original tsc script solves this issue.所以指定原始 tsc 脚本的路径解决了这个问题。

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).您正在使用tsc命令来编译您的打字稿文件,因此,它不会将必要的包代码包含到结果文件(js)中,因此您需要将node_modules文件夹(包含包文件)上传/发布到/home/ site/wwwroot也是(可以通过发布一起发布带有结果文件的node_modules )。

You can compile your typescript file through webpack, which can include packages' code to the result file.你可以通过 webpack 编译你的打字稿文件,它可以将包的代码包含到结果文件中。 https://webpack.js.org/guides/typescript/ https://webpack.js.org/guides/typescript/

解决了

rm -rf your_project/node_modules

我遇到了同样的问题,并通过将 npm 更新到适当的版本(与已安装的 nodejs 版本兼容)并删除了所有节点模块来解决它,然后执行了“sudo npm install typescript -g”和“npm install”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Cloud Functions (Typescript) 中找不到模块 @sendgrid/mail - Cannot find module @sendgrid/mail in Cloud Functions (Typescript) 出现错误:找不到模块“js-yaml” - Getting Error: Cannot find module 'js-yaml' 找不到命名空间“FirebaseFirestore”- Node.js,Express,Typescript - Cannot find namespace 'FirebaseFirestore' - Node.js, Express, Typescript Vue3.js )错误:找不到模块'firebase' - Vue3.js ) Error: Cannot find module 'firebase' 找不到模块'axios' - Cannot find module 'axios' 找不到模块'puppeteer' - Cannot find module 'puppeteer' Twilio function 与 TypeScript 一起部署找不到模块 @twilio-labs/serverless-runtime-types - Twilio function deployed with TypeScript cannot find module @twilio-labs/serverless-runtime-types 错误:在谷歌应用引擎上部署 node.js 后找不到模块“/workspace/server.js” - Error: Cannot find module '/workspace/server.js' upon node js deploy on google app engine #cgo LDFLAGS:找不到 lib 文件 - #cgo LDFLAGS: cannot find lib files 初始化“@nuxtjs/firebase”模块时出现 Nuxt 致命错误 - 找不到模块“firebase/compat/app” - Nuxt FATAL error when initializing '@nuxtjs/firebase' module - Cannot find module 'firebase/compat/app'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM