简体   繁体   中英

Cannot use import statement outside a module babel

Sorry for asking the same question but I could not able to solve this issue after going through many blogs/SO answers..

Package.json is as follows: Here I have already added type module but no luck and also added --experimental-modules flag

     "name": "my-react-app",
     "version": "1.1.1",
     "private": true,
     "type": "module",
     "scripts": {
            .......
           "start": "webpack-dev-server --env.dev",
           "start:server": "node --experimental-modules src/server/bootstrap.js",
            .....
               },
     "dependencies": {
         "@babel/runtime": "^7.10.2",
         "babel-plugin-transform-es2015-modules-systemjs": "^6.24.1",
         "babel-polyfill": "^6.26.0",
         "babel-preset-es2015": "^6.24.1",
         "custom-event-polyfill": "^1.0.6",
         "fbemitter": "^2.1.1",
         "install": "^0.12.1",
          "precise-ui": "^1.3.0",
          "react": "^16.13.1",
         "react-dom": "^16.4.2",
         "react-fast-compare": "^2.0.2",
         "react-open-weather": "^0.3.0",
         "react-router-dom": "^4.3.1",
         "styled-components": "^5.1.1"
         }
        ....
      }

src/server/bootstarp.js I have added presets

  require('ignore-styles');

 require('babel-register')({
 ignore: [ /(node_modules)/ ],
 presets: ["env", "react", "stage-2","es2015","es2016"],
 plugins: []

 });

require('./index');

bable.config.js is as follows:

   {
    "presets": ["env", "react", "stage-2","es2015","es2016"],
   "plugins": [],
    "env": {
     "test": {
     "plugins": [
                 [
                 "istanbul",
                 {
                "include": [
                            "src/**.*"
                           ],
                 "exclude": [
                           "dist/**.*",
                           "node_modules/**.*",
                           "tests/**.*"
                            ]
               }
             ]
           ]
         }
      }
    }

Error I am getting while executing npm run start:server:

        node --experimental-modules src/server/bootstrap.js

        /Users/zospakir/Projects/my-react-app/node_modules/precise-ui/dist/es6/index.js:1
        import * as colors from './colors';
        ^^^^^^

        SyntaxError: Cannot use import statement outside a module
         at wrapSafe (internal/modules/cjs/loader.js:1116:16)
         at Module._compile (internal/modules/cjs/loader.js:1164:27)
        at Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
          at Object.require.extensions.<computed> [as .js] (/Users/zospakir/Projects/my- react-  app/node_modules/babel-register/lib/node.js:152:7) 

Node version: v14.4.0 NPM version: 6.14.5

I have also tried to change bootstrap.js to bootstrap.cjs but it also did not help

try this:

npm install babel-plugin-syntax-dynamic-import --save

bable.config.js set import plugin:

  "plugins": [
     "syntax-dynamic-import"
   ]

refer to link

thanks

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