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

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

Closed
wojtekmaj opened this issue Nov 28, 2023 · 4 comments

Comments

@wojtekmaj
Copy link

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"]
}
@wojtekmaj wojtekmaj changed the title SyntaxError: Named export 'rimraf' not found. The requested module '../data/test_json_file.json' does not provide an export named 'default' Nov 28, 2023
@isaacs
Copy link
Member

isaacs commented Nov 28, 2023

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
Copy link
Author

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

@isaacs isaacs closed this as completed in c72a9d5 Nov 28, 2023
@isaacs
Copy link
Member

isaacs commented Nov 28, 2023

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

Whatever the case, it was easily fixed :)

@isaacs
Copy link
Member

isaacs commented Nov 28, 2023

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.

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