alpacahq / alpaca-ts

A TypeScript Node.js library for the https://alpaca.markets REST API and WebSocket streams.
ISC License
156 stars 41 forks source link

Fails to build in nodejs environment #66

Closed FredrikNoren closed 3 years ago

FredrikNoren commented 3 years ago

Hi! I'm trying to use this library with firebase functions (node 12 environment), but I'm getting a build error from tsc.

It's a newly create firebase functions project (so fairly standard settings). This is my tsconfig file:

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "typeRoots": ["node_modules/@types"]
  },
  "compileOnSave": true,
  "include": [
    "src"
  ]
}

This is the output:


node_modules/@master-chief/alpaca/@types/stream.d.ts:1:23 - error TS2688: Cannot find type definition file for 'ws'.

1 /// <reference types="ws" />
                        ~~

node_modules/@master-chief/alpaca/@types/stream.d.ts:2:8 - error TS1259: Module '"/Users/fredrik/github/garagefunds/functions/node_modules/isomorphic-ws/index"' can only be default-imported using the 'esModuleInterop' flag

2 import WebSocket from 'isomorphic-ws';
         ~~~~~~~~~

  node_modules/isomorphic-ws/index.d.ts:8:1
    8 export = WebSocket
      ~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/@master-chief/alpaca/@types/stream.d.ts:3:8 - error TS1259: Module '"/Users/fredrik/github/garagefunds/functions/node_modules/eventemitter3/index"' can only be default-imported using the 'esModuleInterop' flag

3 import EventEmitter from 'eventemitter3';
         ~~~~~~~~~~~~

  node_modules/eventemitter3/index.d.ts:134:1
    134 export = EventEmitter;
        ~~~~~~~~~~~~~~~~~~~~~~
    This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/isomorphic-ws/index.d.ts:6:28 - error TS7016: Could not find a declaration file for module 'ws'. '/Users/fredrik/github/garagefunds/functions/node_modules/ws/index.js' implicitly has an 'any' type.
  Try `npm install @types/ws` if it exists or add a new declaration (.d.ts) file containing `declare module 'ws';`

6 import WebSocket = require('ws')
                             ~~~~

Found 4 errors.

Any idea what this might be about?

husayt commented 3 years ago

Why don't you try node14, it is supported in firebase.

On Mon, 15 Mar 2021, 15:25 Fredrik Norén, @.***> wrote:

Hi! I'm trying to use this library with firebase functions (node 12 environment), but I'm getting a build error from tsc.

It's a newly create firebase functions project (so fairly standard settings). This is my tsconfig file:

{ "compilerOptions": { "module": "commonjs", "noImplicitReturns": true, "noUnusedLocals": true, "outDir": "lib", "sourceMap": true, "strict": true, "target": "es2017", "typeRoots": @.***"] }, "compileOnSave": true, "include": [ "src" ] }

This is the output:

@.**@./stream.d.ts:1:23 - error TS2688: Cannot find type definition file for 'ws'.

1 /// ~~

@.**@./stream.d.ts:2:8 - error TS1259: Module '"/Users/fredrik/github/garagefunds/functions/node_modules/isomorphic-ws/index"' can only be default-imported using the 'esModuleInterop' flag

2 import WebSocket from 'isomorphic-ws';


  node_modules/isomorphic-ws/index.d.ts:8:1
    8 export = WebSocket
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

@.**@./stream.d.ts:3:8 - error TS1259: Module '"/Users/fredrik/github/garagefunds/functions/node_modules/eventemitter3/index"' can only be default-imported using the 'esModuleInterop' flag

3 import EventEmitter from 'eventemitter3';


  node_modules/eventemitter3/index.d.ts:134:1
    134 export = EventEmitter;
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

node_modules/isomorphic-ws/index.d.ts:6:28 - error TS7016: Could not find a declaration file for module 'ws'. '/Users/fredrik/github/garagefunds/functions/node_modules/ws/index.js' implicitly has an 'any' type. Try npm install @types/ws if it exists or add a new declaration (.d.ts) file containing declare module 'ws';

6 import WebSocket = require('ws')



Found 4 errors.

Any idea what this might be about?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/117/alpaca/issues/66>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAI5PLE2VTGG7KRRBCNYQEDTDYRGVANCNFSM4ZGYPDNQ>
.
FredrikNoren commented 3 years ago

@husayt I just tried changing "engine" in package.json to 14, and I'm also running 15 locally, but still getting the same error as above. This error also happens in the typescript compile step so think it shouldn't matter at that point? (Though could be wrong)

117 commented 3 years ago

Try removing typeRoots from the compilerOptions.

FredrikNoren commented 3 years ago

@117 That gave me a whole lot more errors; it's probably because this project is in a subfolder of another project (that's the default way firebase projects are set up; the "app" as the root project and then the firebase functions in a subfolder of that). I think the typeRoots are there to make sure it's not using the parents typeRoots

husayt commented 3 years ago

@FredrikNoren ok, that narrows the problem . Can you try adding esModuleInterop flag to your ts config?

FredrikNoren commented 3 years ago

@husayt Hm yeah when I do that it removes the stream and eventemitter3 error (though some others appear, but they're related to my code and I can probably fix them). But the Cannot find type definition file for 'ws'. remains.

Edit: Fixed my errors, so with esModuleInterop only these two errors remain:


node_modules/@master-chief/alpaca/@types/stream.d.ts:1:23 - error TS2688: Cannot find type definition file for 'ws'.

1 /// <reference types="ws" />
                        ~~

node_modules/isomorphic-ws/index.d.ts:6:28 - error TS7016: Could not find a declaration file for module 'ws'. '/Users/fredrik/github/garagefunds/functions/node_modules/ws/index.js' implicitly has an 'any' type.
  Try `npm install @types/ws` if it exists or add a new declaration (.d.ts) file containing `declare module 'ws';`

6 import WebSocket = require('ws')
                             ~~~~
117 commented 3 years ago

Looking into a fix.

117 commented 3 years ago

Try installing the types for ws? npm i --save @types/ws

husayt commented 3 years ago

@117 I don't think there is something to fix on our side. I have a typescript project which works just fine. It has to be smth with the way project configured.

Here is a sample tsconfig, which works for me:

{
  "compilerOptions": {
    "baseUrl": ".",
    "module": "ESNext",
    "target": "es2016",
    "lib": ["DOM", "ESNext"],
    "strict": true,
    "esModuleInterop": true,
    "incremental": true,
    "skipLibCheck": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "noUnusedLocals": true,
    "strictNullChecks": true,
    "forceConsistentCasingInFileNames": true,
  },
  "exclude": ["node_modules"]
}
FredrikNoren commented 3 years ago

@117 Doh, yeah installing ws types solved it! Should have tried it but I just assumed they were already included. Thanks!