admin管理员组文章数量:1406943
If I make a script.js and add:
import * as fs from 'node:fs';
And then run with:
node script.js
It gives me a message and then reparses the script, but it still runs:
> Warning: Module type of
> file:///path_to_file/script.js is
> not specified and it doesn't parse as CommonJS. Reparsing as ES module
> because module syntax was detected. This incurs a performance
> overhead. To eliminate this warning, add "type": "module" to NodeJSProject\package.json.
This is understandable. If I change the file extension to anything other than .js, like .ts or .blah. I get the error:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".blah" for path/NodeJSProject\script.blah
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:219:9)
at defaultGetFormat (node:internal/modules/esm/get_format:245:36)
at defaultLoad (node:internal/modules/esm/load:120:22)
at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:514:32)
at async ModuleJob._link (node:internal/modules/esm/module_job:115:19) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
Ordinarily .blah or .ts would run Javascript if run by Node, but if using the import keyword it specifically has to be .js.
Oh, and I know by default Node runs in CommonJS mode, and you should have a package.json to specify module mode, that's why I got the message:
Reparsing as ES module
> because module syntax was detected.
My question is why the .js extension is needed out of curiosity.
If I make a script.js and add:
import * as fs from 'node:fs';
And then run with:
node script.js
It gives me a message and then reparses the script, but it still runs:
> Warning: Module type of
> file:///path_to_file/script.js is
> not specified and it doesn't parse as CommonJS. Reparsing as ES module
> because module syntax was detected. This incurs a performance
> overhead. To eliminate this warning, add "type": "module" to NodeJSProject\package.json.
This is understandable. If I change the file extension to anything other than .js, like .ts or .blah. I get the error:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".blah" for path/NodeJSProject\script.blah
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:219:9)
at defaultGetFormat (node:internal/modules/esm/get_format:245:36)
at defaultLoad (node:internal/modules/esm/load:120:22)
at async ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:514:32)
at async ModuleJob._link (node:internal/modules/esm/module_job:115:19) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
Ordinarily .blah or .ts would run Javascript if run by Node, but if using the import keyword it specifically has to be .js.
Oh, and I know by default Node runs in CommonJS mode, and you should have a package.json to specify module mode, that's why I got the message:
Reparsing as ES module
> because module syntax was detected.
My question is why the .js extension is needed out of curiosity.
Share Improve this question asked Mar 6 at 14:39 ZebrafishZebrafish 15.1k3 gold badges66 silver badges154 bronze badges 6 | Show 1 more comment1 Answer
Reset to default -3Solution 1: Use
const fs = require('node:fs');
instead of
import * as fs from 'node:fs';
Solution 2:
Use
node --input-type=module script.js
instead of
node script.js
本文标签: javascriptWhy does import in node only work with js file extensionStack Overflow
版权声明:本文标题:javascript - Why does import in node only work with .js file extension? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744969033a2635123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
node script.mjs
. – Sergey A Kryukov Commented Mar 6 at 15:12