简体   繁体   中英

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

Faced the same issue due to copying node_modules .

SOLUTION

The following change in package.json solves the issue:

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

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.

So specifying the path to original tsc script solves this issue.

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/

解决了

rm -rf your_project/node_modules

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM