I am trying to build an Angular application that uses xml2js however I am getting the following error:
$ ng build
⠦ Building...
✘ [ERROR] Could not resolve "timers"
node_modules/xml2js/lib/parser.js:36:25:
36 │ setImmediate = require('timers').setImmediate;
╵ ~~~~~~~~
The package "timers" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
I have built the app without any problems previously, however I recently started to get this problem. I didn't change angular.json
or tsconfig.app.json
since then. Also, the app correctly works if I run the dev-server via ng serve
and via ng serve -c production
.
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"<NDA_OMIT>": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "sass"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:application",
"options": {
"outputPath": "dist/<NDA_OMIT>",
"index": "src/index.html",
"browser": "src/main.ts",
"polyfills": [
"zone.js",
"node_modules/setimmediate/setImmediate.js"
],
"allowedCommonJsDependencies": [
"fast-levenshtein"
],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "sass",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/custom-theme.scss",
"src/styles.sass"
],
"stylePreprocessorOptions": {
"includePaths": [
"src/assets/styles"
]
},
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "2mb",
"maximumError": "5mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "8kb",
"maximumError": "16kb"
}
],
"outputHashing": "all",
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.production.ts"
}
]
},
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"proxyConfig": "src/proxy.conf.json"
},
"configurations": {
"production": {
"buildTarget": "<NDA_OMIT>:build:production"
},
"development": {
"buildTarget": "<NDA_OMIT>:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"buildTarget": "<NDA_OMIT>:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": [
"zone.js",
"zone.js/testing"
],
"tsConfig": "tsconfig.spec.json",
"inlineStyleLanguage": "sass",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.sass"
],
"scripts": []
}
},
"lint": {
"builder": "@angular-eslint/builder:lint",
"options": {
"lintFilePatterns": [
"src/**/*.ts",
"src/**/*.html"
]
}
}
}
}
},
"cli": {
"schematicCollections": [
"@angular-eslint/schematics"
]
}
}
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": [
"node",
"@angular/localize"
],
"baseUrl": "./",
"paths": {
"timers": [
"./node_modules/timers-browserify"
],
"stream": [
"./node_modules/stream-browserify"
],
"environment": [
"./src/environments/environment"
]
}
},
"files": [
"src/main.ts"
],
"include": [
"src/**/*.d.ts"
]
}
ng version
outputAngular CLI: 17.2.0
Node: 20.11.0
Package Manager: npm 10.4.0
OS: linux x64
Angular: 17.2.1
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.1702.0
@angular-devkit/build-angular 17.2.0
@angular-devkit/core 17.2.0
@angular-devkit/schematics 17.2.0
@angular/cdk 17.3.7
@angular/cli 17.2.0
@angular/material 17.3.7
@angular/material-moment-adapter 17.3.7
@schematics/angular 17.2.0
rxjs 7.8.1
typescript 5.3.3
zone.js 0.14.4
For some reason, installing timers fixes the issue. xml2js works perfectly fine even if that library doesn't provide setImmediate
.