0

I'm developing a fairly straight forward typescript node app and I wanted to include this library correct-frequency-random-letters. I figured I could just get by using just tsc and running the output with node. However, with stock compiler and "module" set to "commonjs" for my tsconfig, I get this error:

C:\Users\gabek\code\gamejay-bot\node_modules\correct-frequency-random-letters\dist\index.js:16
import FrequencyType from "./frequency/FrequencyType";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1026:15)
    at Module._compile (node:internal/modules/cjs/loader:1061:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1149:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:999:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (C:\Users\gabek\code\gamejay-bot\dist\word-hunt\main.js:4:44)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)

Node.js v17.3.0

As you can see, it's being thrown in the package itself, so I can't futz around with .cjs vs .js file extensions. This means solutions like changing my node project type to "module" or changing my "module" setting to "esnext" in tsconfig seems to just make things worse (in the latter option, it breaks all my imports in the code I've actually written. I'm actually not sure why, but I get this error on all my imports: Did you mean to set the 'moduleResolution' option to 'node', or to add aliases to the 'paths' option?)

I feel like I haven't done anything particularly complicated here. Am I missing something? How would someone do this properly? I even tried using a bundler like Rollup but I ended up with the exact same error and a bunch of other more complicated Rollup-specific solutions to try... but I figured before I went that route I'd ask here.

Here's my repository for reference

Dabe
  • 13
  • 5
  • The error in the latter case is very informative. When `module` is not `commonjs`, you must explicitly set `moduleResolution` to `node` to enable the node convention based module resolution. I always recommend setting `moduleResolution` explicitly to avoid confusion and prevent breakage is when an intermediate tool chain component changes – Aluan Haddad Apr 30 '22 at 16:08
  • I read about this in detail answering this related question https://stackoverflow.com/a/57383664/1915893 – Aluan Haddad Apr 30 '22 at 16:12
  • @AluanHaddad I did try that, and I got a different error. I think the error indicated that my internal imports between my own files were now not working, but I’m afk so I’m unsure. I can post the exact error later. – Dabe Apr 30 '22 at 16:18
  • That's certainly a possibility, just wanted to clarify the resolution issue. – Aluan Haddad Apr 30 '22 at 16:19
  • Here's the error I get when I set `module` to `esnext` and `moduleResolution` to `node`: `(node:80516) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension. (Use 'node --trace-warnings ...' to show where the warning was created) C:\Users\gabek\code\gamejay-bot\dist\main.js:1 import startBot from "./bot"; SyntaxError: Cannot use import statement outside a module` – Dabe Apr 30 '22 at 22:11
  • After setting `type` to `module` in package.json as the above error suggests, I get this `node:internal/errors:464 ErrorCaptureStackTrace(err); ^ Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\gabek\code\gamejay-bot\dist\bot' imported from C:\Users\gabek\code\gamejay-bot\dist\main.js` which is a problem now with my own code... how to I rectify this? – Dabe Apr 30 '22 at 22:14
  • 1
    So I never _really_ figured out the issue. I believe that, basically, this npm package `correct-frequency-random-letters` just compiled to ES5 modules, which include import/export statements. Node doesn't really like that, and when I try to get it to treat those imports as requires, it breaks my actual source code, either the ts or the compiled output, and just generally makes developing a headache. My solution was to copy the source code of the NPM package and include it in my source, so tsc can actually compile it (with credit given, of course). It's a sad fix but a working one. – Dabe May 01 '22 at 03:03
  • Did you find a solution? I think I have a similar issue with another library: osm-pbf-parser-node – ManAnRuck May 29 '23 at 18:17
  • @ManAnRuck Nope, I’m pretty confident in my explanation in my last comment and to this day I’m still just including and compiling the source for this package. – Dabe Jun 02 '23 at 13:07

0 Answers0