SyntaxError: Cannot use import statement outside a module. Really? - node.js

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.

Related

Not able to import mgt elements from Node js application

I am trying to import from the #microsoft/mgt library in my Node js application and facing issues - can someone help. I am already using Babel for using the ES6 modules.
Am I missing something - I am new to Node :(
export * from '#microsoft/mgt-element';
^^^^^^
SyntaxError: Unexpected token 'export'
at wrapSafe (internal/modules/cjs/loader.js:1053:16)
at Module._compile (internal/modules/cjs/loader.js:1101:27)
at Module._compile (C:\Users\dip\Python Projects\Classified_2021\Node_2021\Internet_Projects_2021\node-starter-kit-es6-master\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Object.newLoader [as .js] (\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Module.require (internal/modules/cjs/loader.js:1025:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object. (\src/index.js:2:1)
[nodemon] app crashed - waiting for file changes before starting...
You are importing from microsoft/mgt-element library so change the export to import
import * from '#microsoft/mgt-element';
besides why you are importing the whole library when you only need some parts of it?
if you only need Providers do this:
import {Providers,ProviderState} from '#microsoft/mgt-element';

Node 12 Native import

Im trying to use native import which were released with node 12
but i always get this error even with importing just cors
import * as cors from 'cors';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Module._compile (internal/modules/cjs/loader.js:892:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
at Module.load (internal/modules/cjs/loacjsder.js:812:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
at internal/main/run_main_module.js:17:11
node version v12.13.0
docs https://nodejs.org/docs/latest-v12.x/api/esm.html#esm_code_import_code_specifiers which i tried to follow.
Thanks for help
You have to use this flag --experimental-modules while running node js, also you will have to us .mjs instead of .js extension

Error importing redux:

ERROR:
Error: Cannot find module 'redux'
at Function.Module._resolveFilename (module.js:555:15)
at Function.Module._load (module.js:482:25)
at Module.require (module.js:604:17)
at require (internal/module.js:11:18)
at Object. (C:\dev\Django
code\portfolio_web\stats_frontend\node_modules\react-
redux\lib\connect\mapDispatchToProps.js:8:14)
at Module._compile (module.js:660:30)
at Module._extensions..js (module.js:671:10)
at Object.require.extensions.(anonymous function) [as .js] (C:\dev\Django
code\portfolio_web\stats_frontend\node_modules\babel-
register\lib\node.js:152:7) at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
The file where I import react-redux is reduxStore.js:
import {createStore} from 'react-redux;
//There is nothing else. The function being imported doesn't change anything`
I'm running the file with babel-node, or with webpack-cli (which is using babel to transpile ES2015. In both cases I get the same error.
Tracing the error I can open the source (
...\stats_frontend\node_modules\react-redux\lib\connect\mapDispatchToProps.js of the error (its from official react-redux dist).
Line 8 (which causes the error) I can see a commonJS import:
var _redux = require('redux');
Upon further inspection I can see that the node search algorithm will not find 'redux' because no such file exists in ./node_modules/react-redux/ or in ./node_modules/
I have installed and updated my react-redux installation with node install --save-dev react-redux with no errors.
I was hoping someone can provide insight on why the error occurs and how to fix it
You are importing createStore from wrong library. import it from redux
import { createStore } from 'redux';

Use import keyword without Babel in Node 6

I am wondered why Node.js node index.js raises the following error :
(function (exports, require, module, __filename, __dirname) { import * as math from "lib/math";
^^^^^^
SyntaxError: Unexpected token import
at Object.exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:513:28)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:458:32)
at tryModuleLoad (module.js:417:12)
at Function.Module._load (module.js:409:3)
at Function.Module.runMain (module.js:575:10)
at startup (node.js:160:18)
at node.js:456:3
Even , node -v shows 6.2.1 .
Does import keywords is restricted with Babel , even we use Node 6 (6.2.1) ?
ES 2015 Modules (and therefore the import keyword) are not supported by default in Node.js v6.
You can enable experimental (and almost certainly buggy) module support by launching node with the --harmony_modules flag.
A better option might be to use Babel or a similar tool to transpile ES 2015 modules.
Or you can rewrite your code to not use ES 2015 modules at all.

Import keyword is not recognized by gulp throwing SyntaxError: Unexpected reserved word

Failed to load external module babel-core/register
Failed to load external module babel/register
somelocation\gulpfile.babel.js:27
import fs from 'fs';
^^^^^^
SyntaxError: Unexpected reserved word
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:413:25)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Liftoff.handleArguments (\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:116:3)
at Liftoff.<anonymous> (\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\index.js:192:16)
at module.exports (\AppData\Roaming\npm\node_modules\gulp\node_modules\liftoff\node_modules\flagged-respawn\index.js:17:3)
I am getting this error while trying to run gulp after npm as described here: deploy-appengine.md. How to fix it?
EDIT: as Luke Bennet points out correctly in a comment to this answer, it was indeed JavaScript - the ES6 "import" syntax. The issue is that this won't work out of the box in Node as yet.
You seem to be using the python module import syntax in NodeJS. You should use the NodeJS module require syntax.

Resources