简体   繁体   中英

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

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.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (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

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 compatibility, 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 compatible 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 incompatible 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 completely 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.org/api/packages.html#determining-module-system

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM