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
  • Not reproduced. And it works with node script.mjs. – Sergey A Kryukov Commented Mar 6 at 15:12
  • Can't reproduce, what versions are you using? – GreenSaiko Commented Mar 6 at 15:43
  • @SergeyAKryukov Yes, it works with .js and .mjs extensions, but nothing else. I tried .ts, .blah and .boo. Ordinarily these run in Node without the import statement, but with the import statement it doesn't run, with the message I gave. – Zebrafish Commented Mar 6 at 16:08
  • Then it contradicts your title with “only work with .js file extension”. – Sergey A Kryukov Commented Mar 6 at 18:12
  • @SergeyAKryukov Yes, I didn't know about .mjs, it works for .js and .mjs and any other name it won't work. Interestingly GreeSaiko said they couldn't reproduce. – Zebrafish Commented Mar 6 at 18:17
 |  Show 1 more comment

1 Answer 1

Reset to default -3

Solution 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