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

我的梦境 提交于 2020-08-08 05:29:14

问题


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:

Error: Cannot find module '../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. 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.

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"
]
}

回答1:


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).

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




回答2:


I have faced the same issue and solved it by updating the npm to appropriate version(Compatible with installed nodejs version) and deleted all node modules and then did "sudo npm install typescript -g" and "npm install"



来源:https://stackoverflow.com/questions/57178989/cannot-find-module-lib-tsc-js-when-releasing-typescript-application-to-azur

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!