Click here to Skip to main content
15,849,721 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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 am just looking to run this code and test it locally. Thanks in advance.

What I have tried:

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.
Posted
Updated 24-Feb-21 2:29am

I noticed that your start command (from package.json is:
"start": "node dist/index.js"

Have you tried:
/>npm start <ENTER>
to see if the start command will run?
 
Share this answer
 
I was facing proxy issue while installing npm locally which I have resolved from

https://www.codeproject.com/Questions/5258241/How-to-resolve-NPM-proxy-issue-whilel-running-node

Then I am able to run the application without any issue.
 
Share this answer
 
v4
I fixed the problem adding libraries to control typescript in Node.
Here goes:

yarn add typescript -D
yarn tsc --init
yarn add ts-node-dev -D

now go to package.json and create:
"scripts": {
"start": "tsnd <pathtoyourfile>" //Example: tsnd src/server.ts
}

run yarn start and happy codding
 
Share this answer
 
Using typescript files(with import statements) and webpack to create js files.

tsconfig.json needs to contain the following line:

"module"             : "CommonJS",


This replaces import to require statements in the built js files when running webpack.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900