0

I am relatively new to Node js and was working on .NET MVC. When I am trying to run it using command node index its giving me an below error.

H:\Services\src>node index
H:\Services\src\index.js:1
import app from './app';
^^^^^^

SyntaxError: Cannot use import statement outside a module
?[90m    at Module._compile (internal/modules/cjs/loader.js:895:18)?[39m
?[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)?[39m
?[90m    at Module.load (internal/modules/cjs/loader.js:815:32)?[39m
?[90m    at Function.Module._load (internal/modules/cjs/loader.js:727:14)?[39m
?[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)?[39m
?[90m    at internal/main/run_main_module.js:17:11?[39m

Below is my package.json file. It says it is using node version 8.0.0 and npm version 5.0.0. Bt I have installed node version is v12.14.0 and NPM installed version is 6.13.4.

{
  "name": "core",
  "version": "0.0.1",
  "description": "Core",
  "main": "dist/index.js",
  "engines": {
    "node": "8.0.0",
    "npm": "5.0.0"
  },
  "scripts": {
    "prestart": "npm run -s build",
    "start": "node dist/index.js",
    "dev": "nodemon src/index.js --exec \"node -r dotenv/config -r babel-register\" localdev",
    "clean": "rimraf dist && rimraf -p",
    "build": "npm run clean && mkdir -p dist && babel src -s -D -d dist",
    "test": "jest --watch",
    "lint": "esw -w src test"
  },
  "keywords": [
    "express",
    "babel",
    "es6",
    "es2015",
    "es2016",
    "es2017",
    "eslint"
  ],
  "author": "ABC",
  "license": "UNLICENSED",
  "dependencies": {
    "babel-cli": "6.26.0",
    "babel-plugin-transform-object-rest-spread": "6.26.0",
    "babel-preset-env": "1.6.1",
    "cors": "2.8.5",
    "express": "4.16.4",
    "js-yaml": "3.12.2",
    "mssql": "5.0.0",
    "winston": "3.1.0",
    "winston-daily-rotate-file": "3.5.1"
  },
  "devDependencies": {
    "babel-eslint": "7.2.3",
    "babel-jest": "21.0.2",
    "babel-register": "6.24.1",
    "dotenv": "4.0.0",
    "eslint": "4.10.0",
    "eslint-config-airbnb-base": "12.1.0",
    "eslint-plugin-import": "2.8.0",
    "eslint-plugin-jest": "21.0.2",
    "eslint-watch": "3.1.0",
    "nodemon": "1.18.10",
    "rimraf": "2.6.3"
  },
  "babel": {
    "presets": [
      [
        "env",
        {
          "targets": {
            "node": "current"
          }
        }
      ]
    ],
    "plugins": [
      "transform-object-rest-spread"
    ]
  },
  "eslintConfig": {
    "parser": "babel-eslint",
    "plugins": [
      "import",
      "jest"
    ],
    "parserOptions": {
      "ecmaVersion": 2017,
      "sourceType": "module"
    },
    "env": {
      "node": true,
      "jest": true
    },
    "extends": [
      "eslint:recommended"
    ]
  }
}
I have tried different google solutions for above error but each time gets a diff error depending upon steps I am doing to fix the issue. I am just looking to run this code and test it locally.

Thanks in advance.

11
  • Take a look at the "scripts" sections in your package.json for commands that are intended to run. You start them with npm run <command> so in your case you probably want: npm run dev Commented Jan 30, 2020 at 11:44
  • Your start script calls dist/index.js file, but you are in src folder, go to project root folder and try run script again. Maybe this solves your problem. But you should build your app before running start script Commented Jan 30, 2020 at 11:47
  • @RolandStarke when I ran npm run dev, I again got the different error, as below 'nodemon' is not recognized as an internal or external command, operable program or batch file. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! [email protected] dev: nodemon src/index.js --exec "node -r dotenv/config -r babel-register" localdev npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the [email protected] dev script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. Commented Jan 30, 2020 at 11:59
  • @AhmetZeybek, in my project structure I dont 'dist' folder itself. Commented Jan 30, 2020 at 12:02
  • @Nilesh First install nodemon package globally, then run build script. After build, you must have a dist folder in your project root folder, you can run start script after success build. Anyway, you don't have to install nodemon package for build and start scripts Commented Jan 30, 2020 at 12:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.