admin管理员组

文章数量:1313179

I am a beginner to NodeJS. I was trying to import a module fs.readFileSync I used import { readFileSync } from 'fs' as per the NodeJS docs, But I keep getting the below error,

ERROR----->> SyntaxError: Cannot use import statement outside a module

My Research

->I found that adding type:module in package.json file solves this issue.

My Doubt

->I want to know why this cannot be imported as per documentation??

Code

import { readFileSync } from 'fs';
fs.writeFileSync('notes.txt','This was read by NodeJS')

Full error

SyntaxError: Cannot use import statement outside a module
    at ObjectpileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._pile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

I am a beginner to NodeJS. I was trying to import a module fs.readFileSync I used import { readFileSync } from 'fs' as per the NodeJS docs, But I keep getting the below error,

ERROR----->> SyntaxError: Cannot use import statement outside a module

My Research

->I found that adding type:module in package.json file solves this issue.

My Doubt

->I want to know why this cannot be imported as per documentation??

Code

import { readFileSync } from 'fs';
fs.writeFileSync('notes.txt','This was read by NodeJS')

Full error

SyntaxError: Cannot use import statement outside a module
    at Object.pileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._pile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
Share Improve this question edited Dec 4, 2021 at 11:24 Song Lim 3224 silver badges13 bronze badges asked Dec 4, 2021 at 10:13 devanildevanil 5311 gold badge6 silver badges15 bronze badges 1
  • 1 "Node.js treats JavaScript code as CommonJS modules by default. Authors can tell Node.js to treat JavaScript code as ECMAScript modules via the .mjs file extension, the package.json "type" field, or the --input-type flag. See Modules: Packages for more details." (Source) – Andreas Commented Dec 4, 2021 at 10:19
Add a ment  | 

1 Answer 1

Reset to default 7

By default, for a .js file, nodejs first looks in the package.json file for the module type. If no module type is found there, then it goes by the file extension of the file. It it's a .js file, then it assumes it's a CommonJS module (where you use require(), not import). If it's a .mjs file, then it assumes it's a ESM file (where you use import).

As you've discovered, you can override the handling of file extensions by specifying the type as module in the package.json file so that .js files can use import.

You can tell in your stack trace that the file loading is being done by: internal/modules/cjs/loader where the cjs means CommonJS so it's definitely trying to load the file as a CommonJS module.


If you wonder why things are set up this way, it's because of nodejs history. Originally, all there were was CommonJS files - that was the only module type for many years. So, for backwards patibility, if no module type is specified in either the package.json file or via a special file extension, then nodejs assumes a CommonJS file so that it remains patible with older modules that don't know anything about the newer module types.

As for why you can't use import in CommonJS files, this was clearly a decision that the nodejs architects probably considered carefully. The challenge is that some parts of the ESM module specification are inpatible with some things in the CommonJS module format. So, rather than try to cram the features of both modules into one format, they decided to have two pletely separate formats. This avoids conflicts between the two architectures, but creates this need to figure out which module type is being used.


Here's the nodejs documentation on how nodejs chooses a module type:

https://nodejs/api/packages.html#determining-module-system

本文标签: javascriptNodeJS gt SyntaxError Cannot use import statement outside a moduleStack Overflow