-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
'this' keyword is equivalent to 'undefined' #1518
Comments
@iwinux I had a similar issue. I was able to resolve the warnings by using the rollup-plugin-async before invoking the |
@cfnelson's approach sounds like the right one. It's a warning because sometimes you can ignore it and sometimes you can't. You can specify the behaviour you want with You can also suppress the warning with a custom {
onwarn(warning, warn) {
if (warning.code === 'THIS_IS_UNDEFINED') return;
warn(warning); // this requires Rollup 0.46
}
} |
@iwinux @cfnelson I just ran into this as well. Using a rollup plugin first as you suggested. Thank you! It got rid of the error but it seems like we are masking an underlying problem. @Rich-Harris I think this is the same: It comes from this part of the code:
While using the plugin compiles to this:
In my case I was using So is rollup not compatible with babel's EDIT: Tested using http://facebook.github.io/regenerator/ and it uses a global this, try this example:
|
TLDR: an unbound arrow function will cause this issue to arise. Use a plain function or put your async arrow function in a bounding structure. It isn't async/awaits fault at all. FWIW, and to anyone still trying to solve this issue who is using babel and does not want to add the (unnecessary) rollup-plugin-async, I figured out the underlying problem. Basically I had an async arrow function that I was transpiling which was causing the const asyncFunc = async () => {
const data = await fetchMyData();
return data;
} This will get transpiled by rollup and produce the offending error. Why? Arrow functions use a lexically bound You're probably saying, "But there is no var _this = undefined;
var asyncFunc = function() {
var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
return regeneratorRuntime.wrap(function _callee$(_context) {
...
}, _callee, _this);
}));
return function (_x3) {
return _ref.apply(this, arguments);
};
} Note the How to fix it? The easiest way is to just not use an arrow function. Instead of what you had above use: const asyncFunction = async function() {
const data = await getMyData();
return data;
} The babel transpiler will appropriately use the block scoped var asyncFunction = function() {
var _ref = asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
...
}, _callee, this);
}));
return function dumFunc() {
return _ref.apply(this, arguments);
};
} As it were, arrow functions aren't the be-all end-all for functions in all cases, especially where the block scoped |
Saw this after adding typescript to my sapper project. Adding |
Thanks for sharing. I wonder if I just ignore the warning, it seems that my bundle should still work. How can i judge if it break the runtime logic when this warning show up. |
For anyone else that stumbles across this, I was having a similar error message when building a SvelteKit project for production. Specifically complaining about $ npm ls @opentelemetry/api
[email protected] /workspace
├─┬ @azure/[email protected]
└─┬ @azure/[email protected]
└── @opentelemetry/[email protected] These were the kind of errors being produced (the circular dependency error was also new thanks to the introduction of that Azure package): Output$ npm run build
> [email protected] build
> vite build
vite v5.2.10 building SSR bundle for production...
✓ 155 modules transformed.
[My app output...]
vite v5.2.10 building for production...
✓ 139 modules transformed.
[Client build output...]
✓ built in 1.99s
[Server build output...]
✓ built in 8.29s
Run npm run preview to preview your production build locally.
> Using @sveltejs/adapter-node
node_modules/@opentelemetry/api/build/esm/metrics/NoopMeter.js (16:17): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/metrics/NoopMeter.js (16:25): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/diag.js (16:14): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/diag.js (16:22): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/diag.js (32:21): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/diag.js (32:29): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/context.js (16:14): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/context.js (16:22): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/context.js (32:21): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/api/context.js (32:29): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/baggage/internal/baggage-impl.js (16:14): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/baggage/internal/baggage-impl.js (16:22): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/baggage/internal/baggage-impl.js (32:16): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/baggage/internal/baggage-impl.js (32:24): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/context/NoopContextManager.js (16:14): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/context/NoopContextManager.js (16:22): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/context/NoopContextManager.js (32:21): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/context/NoopContextManager.js (32:29): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js (16:14): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js (16:22): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js (32:21): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
node_modules/@opentelemetry/api/build/esm/diag/ComponentLogger.js (32:29): The 'this' keyword is equivalent to 'undefined' at the top level of an ES module, and has been rewritten
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLElement.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLElement.js -> node_modules/xmlbuilder/lib/XMLAttribute.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLCData.js -> node_modules/xmlbuilder/lib/XMLCharacterData.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDeclaration.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDocType.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDocType.js -> node_modules/xmlbuilder/lib/XMLDTDAttList.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDocType.js -> node_modules/xmlbuilder/lib/XMLDTDEntity.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDocType.js -> node_modules/xmlbuilder/lib/XMLDTDElement.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDocType.js -> node_modules/xmlbuilder/lib/XMLDTDNotation.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLRaw.js -> node_modules/xmlbuilder/lib/XMLNode.js
Circular dependency: node_modules/xmlbuilder/lib/XMLNode.js -> node_modules/xmlbuilder/lib/XMLDummy.js -> node_modules/xmlbuilder/lib/XMLNode.js
✔ done Moving |
Hi,
Seems that every definition of async function that gets transformed by Babel (
transform-async-to-generator
in particular) will trigger Rollup to emit this warning message:Can this be safely ignored, or needs some fix? Demo repo here: https://github.com/iwinux/rollup-this-undefined
The text was updated successfully, but these errors were encountered: