-
Notifications
You must be signed in to change notification settings - Fork 331
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
Bad export of generated JavaScript file #1404
Comments
Hi, thanks for reporting. I tried both In the mean time, it does seem that the following works in Typescript files using CJS imports: import ncu from 'npm-check-updates'
const upgraded = await ncu({
// ...
}) |
With this export async function run(
runOptions: RunOptions = {},
{ cli }: { cli?: boolean } = {},
): Promise<PackageFile | Index<VersionSpec> | void> {
// ...
}
export default { run }
So, import ncu from "npm-check-updates";
ncu.run({}); |
Yes, but then There is a major version coming out soon, so it's okay to be break |
To export by default, use the async function run(
runOptions: RunOptions = {},
{ cli }: { cli?: boolean } = {},
): Promise<PackageFile | Index<VersionSpec> | void> {
// ...
}
export = run But it will do a breaking change: import ncu from 'npm-check-updates'
const upgraded = await ncu({
// Pass any cli option
packageFile: '../package.json',
upgrade: true,
// Defaults:
// jsonUpgraded: true,
// silent: true,
})
console.log(upgraded) // { "mypackage": "^2.0.0", ... } |
The index file also does It seems that there isn't a trivial fix here, but I'm definitely interested in switching to a default export if we can figure out how to make it work with both esm and cjs. I'm open to a pull request if you have a better grasp of typescript and esm than me.
Out of curiosity, does this work for you currently? |
import ncu from 'npm-check-updates'
console.log(ncu);
const upgraded = await ncu({
// ...
}) Here's the terminal when I run the JavaScript code above:
|
Okay, interesting. So it's just the types that are wrong. |
I think it should be fine with the following code: async function run(
runOptions: RunOptions = {},
{ cli }: { cli?: boolean } = {},
): Promise<PackageFile | Index<VersionSpec> | void> {
// ...
}
run.run = run;
export = run Here are the tests I ran.
"use strict";
function run() { return "foo"; }
run.run = run;
module.exports = run;
declare function run(): string;
declare namespace run {
var run: typeof import("./ncu");
}
export = run;
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var ncu_1 = __importDefault(require("./ncu"));
console.log((0, ncu_1.default)());
console.log(ncu_1.default.run());
|
Could you open a PR? That would be a big help. |
I tried with
So I deleted
I've added
I don't think this is possible as long as the TypeScript bug hasn't been fixed. TypeScript generates inconsistencies between the JavaScript and the declaration file only for CJS files. Another solution would be to generate ESM files. In this TS Playground, JS and .D.TS are consistent. But I don't know about compatibility with older versions of Node. |
Thanks for investigating. I don't have time at the moment to investigate further but I'll keep this issue open in case anyone else can help. |
npm-check-updates
node >= 14.14
Steps to Reproduce
package.json
index.ts
Steps:
npm install
npx tsc index.ts
Current Behavior
Expected Behavior
No error.
src/index.js
build/src/index.js
build/src/index.d.ts
The JavaScript file generated isn't good because it's not possible to convert an ESM which has a default export and a named export. A CJS only has a default export (which may be an object to simulate named exports).
exports.default = run;
exports a property"default"
in default object exported.I think you only need to export a default object with the
run
property:for JavaScript generation to be correct;
and to don't break backward compatibility.
The text was updated successfully, but these errors were encountered: