英文:
How to import a Javascript file that is ESM from a CommonJS module? Got. Error: Not supported
问题
Your code appears to be written in CommonJS, and you're trying to use the ESM (ECMAScript Modules) syntax to import the 'got' library, which seems to be causing an error. If you want to use ESM in a CommonJS project, you might need to set up ESM support in your Node.js environment.
你的代码似乎是使用CommonJS编写的,而你尝试使用ESM(ECMAScript模块)语法来导入'got'库,这似乎导致了错误。如果你想在CommonJS项目中使用ESM,你可能需要在你的Node.js环境中设置ESM支持。
Here are some steps you can follow:
以下是一些你可以按照的步骤:
-
Update Node.js: Ensure you are using a Node.js version that supports ESM. You mentioned you are using Node Version 12.14.1, which should support async/await, but you might want to consider updating to a more recent version.
-
更新Node.js:确保你正在使用支持ESM的Node.js版本。你提到你正在使用Node版本12.14.1,它应该支持async/await,但你可能想考虑更新到更近期的版本。
-
Configure Package.json: In your project's package.json file, you can specify
"type": "module"
to indicate that your project uses ESM. -
配置Package.json:在你的项目的package.json文件中,你可以指定
"type": "module"
,以表示你的项目使用ESM。 -
Adjust Imports: Make sure your import statements for 'got' and other ESM modules follow the ESM syntax:
-
调整导入:确保你对'got'和其他ESM模块的导入语句遵循ESM语法:
import got from 'got';
By following these steps, you should be able to use ESM modules in your CommonJS project. Remember to update your Node.js version if needed, and make sure your package.json is properly configured for ESM support.
按照这些步骤,你应该能够在你的CommonJS项目中使用ESM模块。如果需要的话,请记得更新你的Node.js版本,并确保你的package.json正确配置了ESM支持。
英文:
My project is written entirely in CommonJS module and I can't change it. I am trying to use a library which is an ESM. The library is Got library (https://github.com/sindresorhus/got).
This is my code
const request = require('request');
const Promise = require('bluebird');
// require('./wrapped-got');
var wrappedgot = null;
function HTTPRequestV2(hostURL, defaultOptions) {
this.hostUrl = hostURL;
this.requestWrapper = request.defaults(defaultOptions);
}
HTTPRequestV2.prototype.init = async function(){
wrappedgot = await import('got');
/*return import('got')
.then(({default: theDefault}) => {
wrappedgot= theDefault;
console.log(theDefault);
});
console.log(wrappedgot);*/
};
But on running this I get error Not Supported on the wrappedgot = await import('got'); line
I tried using the work around of dynamic import function as suggested in their Issues page but its failing with above error
https://github.com/sindresorhus/got/issues/1789
Even tried running their sample code but that is also failing with same error
https://gist.github.com/szmarczak/0f2b70b2a1ed52a56a6d391ac02d094d
------Update-------
I am using Node Version 12.14.1, it supports async & await.
I have read in SO where it has been used in Node Version 10
https://stackoverflow.com/questions/58858782/using-the-dynamic-import-function-on-node-js
Got Library version is 13.0.0
答案1
得分: 0
Node.js 12.x 不支持动态导入,该功能从 Node 13.2.0 引入。另外,Node.js 12.x 的安全支持已经结束了一年多。请升级到当前版本的 Node.js。
英文:
Node.js 12.x does not support dynamic imports which were introduced with Node 13.2.0. Additionally, it has been over a year since security support for Node.js 12.x ended.
Upgrade to a current version of Node.Js.
答案2
得分: -1
I'm pretty sure imports have to be in the global scope and you're trying to use an import
inside of a function. You will have to move that import
outside of that function in order for the error to go away. As a solution to this, import
'got' at the top with those requires and assign it to a variable and then when the function HTTPRequestV2.prototype.init
is run then you should set 'wrappedgot' to whatever variable holds the 'got' import. I hope this helps.
<------------------ General improvements to this code ------------------>
Also this is unrelated but you've named a variable at the top 'Promise' and that should really be avoided since a promise is a constructor function in JavaScript and is used for something else, by using its name for something else it could result in actual promises to throw an error.
I would also suggest not using 'var' in this day and age since it's a legacy version of JavaScript and can result in unexpected bugs that you might not expect unless you know what you are doing.
I would also recommend using import
instead of require to stay up to date with the latest implementations.
英文:
I'm pretty sure imports have to be in the global scope and you're trying to use an import
inside of a function. You will have to move that import
outside of that function in order for the error to go away. As a solution to this, import
'got' at the top with those requires and assign it to a variable and then when the function HTTPRequestV2.prototype.init
is run then you should set 'wrappedgot' to whatever variable holds the 'got' import. I hope this helps.
<------------------ General improvements to this code ------------------>
Also this is unrelated but you've named a variable at the top 'Promise' and that should really be avoided since a promise is a constructor function in JavaScript and is used for something else, by using it's name for something else it could result in actual promises to throw an error.
I would also suggest not using 'var' in this day and age since it's a legacy version of JavaScript and can result in unexpected bugs that you might not expect unless you know what you are doing.
I would also recommend on using import
instead of require to stay up to date with the latest implementations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论