-
Notifications
You must be signed in to change notification settings - Fork 45
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
Add ES6 Style Exports #104
Comments
@ColbyTresness - you brought this up, but putting here for posterity: |
Assigning myself so I don't forget about this |
I expect this is what this is tracking but any thoughts on why when I use things like: import foo from './foo.js'
export default function() { } I get errors in functions, where as for other node projects where I use node 10 it seems to work? VS Code is telling me something about CommonJS and I frankly don't know where CommonJS, ES6, and Node overlap and if this is related 😕 . Seems from link above seems like we are waiting for something with node? EDIT: I think I may understand this now. Have only used this in projects with Babel before so I think it was converting to a node compatible format as node doesn't fully adhere to ES6 it appears? Who knows.. and I thought .NET Core / .NET Standard was strange. Is weird that VS Code prompts me to change this though. |
@jeffhollan You're correct - Node currently just supports CJS style exports (that's |
@jeffhollan Yes, the fact that VS Code prompts for this is a bug that I brought up with @fiveisprime. We left this issue open though since we'd like to support this once Node does. |
Nice thanks. @fiveisprime would be nice if any related code issue we could link |
You might try using
// index.js
// ESM polyfill lets us write source code with Modules, while running CJS
// App code is in main.js.
// Set options as a parameter, environment variable, or rc file.
require = require('esm')(module/* , options*/);
module.exports = require('./main.js'); // main.js
export default async function(context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.query.name || (req.body && req.body.name)) {
context.res = {
// status: 200, /* Defaults to 200 */
body: `Hello ${req.query.name || req.body.name}`,
};
} else {
context.res = {
status: 400,
body: 'ESM: Please pass a name on the query string or in the request body',
};
}
} |
Is it okay to write |
Flagging this as blocked for now. @mhoeger will provide an update and assign it to the correct milestone once we have more information. |
Hi, Quick check if there are any updates. Thank you! |
This is on the way. We’re adding support for ES modules. If you use the |
Any updates on this? |
Yes please see our docs for more details. It’s unclear if ES modules will become stable in Node.js 14 so this feature may keep its “preview” label in Azure Functions until Node.js 16 LTS is available on the platform. https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-node?tabs=v2#ecmascript-modules |
Awesome thank you :) |
@anthonychu should this be out of Preview now? ES Modules are stable in Node.js 16 now: https://nodejs.org/docs/latest-v16.x/api/esm.html#modules-ecmascript-modules |
Yes this should probably be out of preview by now - we will look into it |
Hey, since not just Node 16 LTS is stable now but also 18 LTS, how is this ESM support coming? ESM is becoming the new standard. |
MAJOR EDIT: I've cleaned up my comment heavily as it showed TWO wrong ways of doing this. Wow. I'm running an ESM stack with TypeScript. Got this error when trying to start my func:
My import { HttpResponse, app } from '@azure/functions';
app.http('home', {
methods: ['GET'],
authLevel: 'anonymous',
handler: (): HttpResponse => {
return new HttpResponse({
status: 200,
jsonBody: { msg: 'Test succeeded' },
});
},
route: '',
}); And my EDIT: bad code and conclusions removed, replaced by correct code that actually works: import { HttpResponseInit, app } from '@azure/functions';
app.http('home', {
methods: ['GET'],
authLevel: 'anonymous',
handler: (): HttpResponseInit => {
return {
status: 200,
jsonBody: { msg: 'Test succeeded' },
};
},
route: '',
}); |
Will update issue with more information later on.
The text was updated successfully, but these errors were encountered: