Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No valid exports main found for 'C:\ha-lib\node_modules\home-assistant-js-websocket' #113

Closed
alexciesielski opened this issue Mar 27, 2020 · 4 comments

Comments

@alexciesielski
Copy link

alexciesielski commented Mar 27, 2020

I am trying to integrate this client into my NodeJS library but I am having the following issue when compiling and running my (currently very simple) app.

Repo can be found here: https://github.com/ciesielskico/home-assistant-rxjs

FIrst things first:

  • home-assistant-js-websocket: v5.1.0
  • ws: v7.2.3
  • Node: v13.8.0
  • Typescript: v3.8.3

Error: No valid exports main found for 'C:\ha-lib\node_modules\home-assistant-js-websocket'
at resolveExportsTarget (internal/modules/cjs/loader.js:632:9)
at applyExports (internal/modules/cjs/loader.js:500:14)
at resolveExports (internal/modules/cjs/loader.js:549:12)
at Function.Module._findPath (internal/modules/cjs/loader.js:664:22)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:967:27)
at Function.Module._load (internal/modules/cjs/loader.js:862:27)
at Module.require (internal/modules/cjs/loader.js:1040:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object. (C:\ha-lib\src\index.ts:1:1)
at Module._compile (internal/modules/cjs/loader.js:1151:30)

tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "lib": ["es6"],
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}
src/index.ts

import {
  createConnection,
  createLongLivedTokenAuth,
} from 'home-assistant-js-websocket';
import { from } from 'rxjs';
import { createSocket } from './connection/connection';

export class HomeAssistant {
  async initialize() {
    const auth = createLongLivedTokenAuth(
      'ws://homeassistant:8123',
      'my_token',
    );
      
    return await createConnection({
      createSocket: () => createSocket(auth, true),
    });
  }
}

const connection = from(new HomeAssistant().initialize());
connection.subscribe(console.log);

The createSocket function is taken from the link in your example for NodeJS applications.

I have googled the error message but to my surprise could find only a handful of posts matching that exact error related to some problems with merge-anything package.

I'm pretty sure I'm doing something wrong but I just cannot see what it is, maybe you guys will?

@alexciesielski
Copy link
Author

alexciesielski commented Mar 27, 2020

I just downgraded node using nvm to 11.14.0 and the error disappeared 🤔 so it must be something with latest nodejs.

@balloob
Copy link
Member

balloob commented Mar 27, 2020

We are leveraging the new exports field that the new node supports:

"exports": {
"import": "dist/index.js",
"default": "dist/haws.umd.js"
},

For runtimes that can resolve import, it should point at index.js, which for 5.1.0 looks like this: https://unpkg.com/browse/[email protected]/dist/index.js

All others are pointing at a build in UMD format so supports module.exports, require etc and can be found here https://unpkg.com/browse/[email protected]/dist/haws.umd.js

From your stack trace it looks like it is using the commonjs importer for our package.

Can you try the following with Node 13: open your node_modules/home-assistant-js-websocket/package.json and remove the line that says "type": "module",

@alexciesielski
Copy link
Author

alexciesielski commented Mar 30, 2020

I'm not sure what I have changed over the weekend when working with NodeJS 11 but I reinstalled NodeJS 13 (13.12.0 this time as opposed to 13.8.0) again and I am getting the following error:

tsnd --respawn src/index.ts --host http://homeassistant:8123

Using ts-node version 8.8.1, typescript version 3.8.3
Error [ERR_INVALID_PACKAGE_TARGET]: Invalid "exports" main target "dist/haws.umd.js" defined in
the package config /C:/Users/Alex/Documents/homeassistant-rxjs/node_modules/home-assistant-js-websocket\package.json
at resolveExportsTarget (internal/modules/cjs/loader.js:574:13)
at resolveExportsTarget (internal/modules/cjs/loader.js:621:20)
at applyExports (internal/modules/cjs/loader.js:503:14)
at resolveExports (internal/modules/cjs/loader.js:541:12)
at Function.Module._findPath (internal/modules/cjs/loader.js:661:22)
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:963:27)
at Function.Module._load (internal/modules/cjs/loader.js:859:27)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object. (C:\Users\Alex\Documents\homeassistant-rxjs\src\connection\connection.ts:8:1)

Removing "type": "module" does not change the error.

Quick google search tells me this:

ERR_INVALID_PACKAGE_TARGET#
The package.json exports field contains an invalid target mapping value for the attempted module resolution.

https://nodejs.org/api/errors.html#errors_err_invalid_package_target

Changing the exports.default property to dist/index.js doesn't change the error message (besides the file name now being dist/index.js of course).

@alexciesielski
Copy link
Author

Something interesting: removing the "export" property altogether from your package.json fixes the error in Node 11, 12 and 13.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants