tapjs / tsimp

https://tapjs.github.io/tsimp/
Other
508 stars 12 forks source link

The requested module '../data/test_json_file.json' does not provide an export named 'default' #3

Closed wojtekmaj closed 11 months ago

wojtekmaj commented 11 months ago

I tried replacing ts-node with tsimp in a Node.js server project.

Upon running node --import=tsimp/import src/index.js, I was met with the following error:

/server/src/index.js:2
import myJsonFile from '../data/test_json_file.json' assert { type: 'json' };
       ^

SyntaxError: The requested module '../data/test_json_file.json' does not provide an export named 'default'
    at ModuleJob._instantiate (node:internal/modules/esm/module_job:131:21)
    at async ModuleJob.run (node:internal/modules/esm/module_job:213:5)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)

Node.js v20.9.0

This obviously works when using ts-node.

I have also tried using new with instead of assert, but this didn't do the trick either.

tsconfig.json:

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false,
    "esModuleInterop": true,
    "isolatedModules": true,
    "lib": ["es2023"],
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "noEmit": true,
    "noUncheckedIndexedAccess": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "target": "es2022",
    "verbatimModuleSyntax": true
  },
  "exclude": ["dist"]
}
isaacs commented 11 months ago

Thanks for the report!

Hm, I'm getting a somewhat different error, but yes, json seems like it doesn't work at the moment.

$ node ./dist/esm/bin.mjs x.ts
file:///Users/isaacs/dev/tapjs/tsimp/package.json:2
    "name": "tsimp",
          ^

SyntaxError: Unexpected token ':'
wojtekmaj commented 11 months ago

Do you have "resolveJsonModule": true, in your tsconfig? This might be the reason we're getting different errors.

isaacs commented 11 months ago

I did, but maybe it's "type": "module" in package.json, or different ts versions?

Whatever the case, it was easily fixed :)

isaacs commented 11 months ago

Oh! No, it's that your json happened to be valid JS, and mine failed to parse as JS.

So it's the same failure, just different data.