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
Cannot import ESM modules when using TypeScript #4447
Comments
I looked into the source code and found:
I tested by setting If you're interested in fixing it and submit a PR, it's as easy as fixing https://github.com/knex/knex/blob/master/lib/migrations/util/is-module-type.js to also check filepath that ends with const { readFile } = require('./fs');
module.exports = async function isModuleType(filepath) {
...
return process.env.npm_package_type === 'module' || filepath.endsWith('.mjs') || filepath.endsWith('.ts');
}; However, I think the code above isn't solid enough. By right, it should check if the filepath ends with any value configured in |
@cayter Thank you for looking into it! Would you consider sending a PR for this? |
not sure this is totally relevant, but seems like https://github.com/cfware/get-package-type#readme may be a robust alternative to using npm_package_json.. especially given that the yarn 2 core dev(s?) seem opposed to adding that variable. as a result, i'm guessing this is currently broken with yarn 2. see: |
I've created a PR for this now, I hope this solves it for everybody: #5071 |
With #5071 merged (81b7edd), I believe this issue can be closed now, cc @kibertoad |
Released in 1.0.5 |
Environment
Knex version: 0.95.4
Database + version: sqlite3 5.0.2
OS: macOS Big Sur 11.1
@lorefnon (since related to TypeScript)
Hi there, I spent quite a bunch of time on this without finding the proper way to resolve it.
Bug
When trying to
import randomInt from "random-int"
from a TypeScript migration file we get an error.Running with --esm sort of works but breaks TypeScript:
export async function up(knex: Knex): Promise<void> { ^ SyntaxError: Invalid or unexpected token at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
Thanks!
The text was updated successfully, but these errors were encountered: