I've created and published a Typescript package. When I try to import a dependent module, instead of looking in node_modules
, it looks for it in the current folder, and of course fails to find it. This happens when I install the module from npm in a new project and try to build it.
I'm using Node 18.18
Error:
./node_modules/@namespace/module/dist/esm/odata2ts.config.js:1:0-54 - Error: Module not found: Error: Can't resolve '@odata2ts/odata2ts' in 'D:\Dev\Angular Test\my-app\node_modules\@namespace\module\dist\esm'
The module is installed in node_modules
as expected, I'm unclear why it wouldn't look in the usual location. No other dev dependency seems to be doing this.
This is the file throwing the error, which is being compiled from a .ts file in the main package's build phase before being published.
odata2ts.config.js:
import { EmitModes, Modes } from '@odata2ts/odata2ts';
const config = {
mode: Modes.all,
emitMode: EmitModes.ts,
services: {
namespace: {
sourceUrl: 'https://some.url',
source: 'src/resource/namespace.xml',
output: 'src/build/namespace',
prettier: true,
},
},
};
export default config;
My package.json for the published module:
{
"name": "@namespace/module",
"version": "1.0.0",
"description": "",
"main": "dist/cjs/src/index.js",
"module": "dist/esm/src/index.js",
"umd:main": "dist/umd/src/index.js",
"types": "dist/types/src/index.d.js",
"scripts": {
"postinstall": "husky install",
"prepublishOnly": "pinst --disable",
"postpublish": "pinst --enable",
"gen-odata": "odata2ts",
"build": "npm run gen-odata && npm run build:cjs && npm run build:esm && npm run build:umd && npm run build:types",
"build:cjs": "node tools/cleanup cjs && tsc -p config/tsconfig.cjs.json",
"build:esm": "node tools/cleanup esm && tsc -p config/tsconfig.esm.json",
"build:umd": "node tools/cleanup umd && webpack --config config/webpack.config.js",
"build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json",
"clean": "node tools/cleanup",
"package": "npm run build && npm pack",
"test": "jest --no-cache --runInBand",
"test:cov": "jest --coverage --no-cache --runInBand",
"addscope": "node tools/packagejson name @namespace/module"
},
"keywords": [],
"publishConfig": {
"access": "public"
},
"files": [
"dist"
],
"author": "",
"license": "MIT",
"homepage": "",
"repository": {
"type": "git",
"url": "git@github.com:namespace/module.git"
},
"bugs": {
"url": ""
},
"devDependencies": {
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@odata2ts/odata2ts": "^0.31.1",
"@types/jest": "^29.5.5",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"husky": "^7.0.2",
"jest": "^29.7.0",
"pinst": "^2.1.6",
"prettier": "^2.4.0",
"ts-jest": "^29.1.1",
"ts-loader": "^9.2.5",
"typescript": "^4.4.3",
"webpack": "^5.89.0",
"webpack-cli": "^4.8.0"
},
"dependencies": {
"@odata2ts/http-client-fetch": "^0.6.2",
"@odata2ts/odata-service": "^0.17.1"
}
}
And for the local application:
{
"name": "my-app",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
},
"private": true,
"dependencies": {
"@angular/animations": "^16.2.0",
"@angular/common": "^16.2.0",
"@angular/compiler": "^16.2.0",
"@angular/core": "^16.2.0",
"@angular/forms": "^16.2.0",
"@angular/platform-browser": "^16.2.0",
"@angular/platform-browser-dynamic": "^16.2.0",
"@angular/router": "^16.2.0",
"@namespace/module": "^1.0.2",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^16.2.1",
"@angular/cli": "~16.2.1",
"@angular/compiler-cli": "^16.2.0",
"@types/jasmine": "~4.3.0",
"jasmine-core": "~4.6.0",
"karma": "~6.4.0",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.0",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "~2.1.0",
"typescript": "~5.1.3"
}
}
If any other information is needed, let me know.
The issue ended up being that @odata2ts was a devDependency and needed to be a dependency instead.