nuxt
nuxt copied to clipboard
esm - import modules (mjs, mts,..) or setting package.json's type to "module" not working as exepcted
Environment
- Operating System:
Linux - Node Version:
v18.9.0 - Nuxt Version:
3.0.0-rc.9 - Nitro Version:
0.5.1 - Package Manager:
[email protected] - Builder:
vite - User Config:
srcDir,typescript,head,loading,css,middleware,runtimeConfig,tailwindcss,modules,env,build - Runtime Modules:
@nuxtjs/[email protected],~/modules/dashboardcore.mts,~/modules/auto-import-eslint.ts - Build Modules:
-
Reproduction
(will update later)
Describe the bug
I'm running into different issues when trying to use modules inside of the nuxt3 project.
this one is the one I'm struggling the most: I'm importing a module (mts), but it says I cannot use import statements outside a module. This doesn't make sense in any way.
e.g. [jiti] [unknown] /home/simon/Dev/hokify/hokify-dashboard/src/modules/dashboardcore.mts 13:38:16
ERROR Cannot start nuxt: Cannot use import statement outside a module 13:38:16
import fs from 'fs';
Additional context
Shoudl it be possible to use mts / mjs, or is there anything that I'm missing here?
Logs
No response
Another issue for example, I have set "type": "module" in my package.json of the nuxt project. But using top level await results into:
ERROR Cannot start nuxt: await is only valid in async functions and the top level bodies of modules 10:20:16
const sentryConfig = await Promise.resolve().then(() => require('../config/SentryAPI/sentryToken.json'));
^^^^^
1.) my import statment gets rewritten to an require 2.) it just ignores that I have set type: module..
Thanks for making isssue. A reproduction would be nice to pin point what exactly is the issue, but generally, currently we only avoid jiti transform when path to the (resolved) module has explicit .mjs extension which is highly adice to do while packaging. here is relevant code and we might support type:module from package.json too...
Ah thanks, very interesting, yeah the type:module flag will help me a lot, or any option to overrule this behaviour. A flag for "full" ESM mode would be even better, right now there are several strange things regarding ".ts" and ".js" extensions while importing. Running the same code with node/ts-node and nuxt is quite impossible.
I willl work on a minimal repoduction with the most common cases.
Hey there! Please add a minimal reproduction so we can look into that properly, thank you 🙏🏻💚
Going to mark this as can be closed for now, feel free to reopen if it still persists!
I'm closing this as we don't have a reproduction. But please feel free to add one and I'll happily reopen. We have, however, made considerable improvements to https://github.com/unjs/jiti 🤞