'SyntaxError: Cannot use import statement outside a module. Really?

Exact same ask as SyntaxError: Cannot use import statement outside a module, but much simplified to focus on one specific aspect of the problem.

So, if I'm writing my nodejs based apps, is it really so that I cannot use the new EMS import statements there?

Here is the extremely simplified version of my nodejs based app (index.js):

import Database from 'better-sqlite3';
const db = new Database('foobar.db', options);

Here is what I get:

(node:1) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/app/index.js:1
import Database from 'better-sqlite3';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1032:15)
    at Module._compile (node:internal/modules/cjs/loader:1067:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155: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:77:12)
    at node:internal/main/run_main_module:17:47

I do want to load the ES module using the new EMS import syntax, but I'm not writing a module myself, but an app instead. Thus I feel wrong to put "type": "module" in the package.json or use the .mjs extension, which stresses that its an ES module.

It just doesn't feel right -- nobody has called an express web application a module before.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source