0

I have my project in node.js and express.js. i need to make an executable of node.js project.

Project configuration :

Using ES6 features : import and dynamic import, and for this babel.js , i am using.

Project status :

Project is running correctly no problem.

Transpiling of the project is working. That is bundling creation is ok.

Problem/Error/Issue :

while packaging with command :

pkg . --debug

it giving error of ' import'

package.json configuration :

{
  "name": "server",
  "version": "0.0.0",
  "private": true,
  "bin": "./app.js",
  "main":"./app.js",
  "engines": {
    "node": ">=6"
  },
  "scripts": {
    "start": "nodemon app.js --exec babel-node",
    "test": "nodemon NODE_ENV=development",
    "build": "webpack --config build/webpack.config.js --progress true --display-error-details true"
  },
  "dependencies": {
    "nodemailer": "^4.6.6",
    "protractor": "^5.3.2",
    "pug": "^2.0.3",
    "request": "^2.79.0",
    "serve-favicon": "*",
    "underscore": "^1.9.1",
    "winston": "^2.4.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.2",
    "@babel/core": "^7.1.2",
    "@babel/node": "^7.0.0",
    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
    "@babel/plugin-transform-runtime": "^7.1.0",
    "@babel/preset-env": "^7.1.0",
    "@babel/register": "^7.0.0",
    "babel-loader": "^8.0.4",
    "babel-plugin-dynamic-import-node": "^2.1.0",
    "html-webpack-plugin": "^3.2.0",
    "nodemon": "*",
    "pkg": "^4.3.4",
    "rimraf": "^2.6.1",
    "script-ext-html-webpack-plugin": "^2.0.1",
    "webpack": "^4.20.2",
    "webpack-cli": "^3.1.2"
  },
  "pkg": {
    "scripts": [
      "node_modules/*",
      "build/output/main.bundle.js"
    ],
    "assets": [
      "views/**/*",
      "public/*"
    ],
    "targets": [
      "node8"
    ]
  }
}

webapck.config.js :

module.exports = {
   node: {
    __dirname: true,
    __filename: true
 },
 target: 'node',
  mode: 'development', 
  entry: ["./app.js"],
  output: {
    filename: '[name].bundle.js',
    path: path.resolve(__dirname, 'output')    
  },
  module: {
    rules: [
      {
        test: /\.css$/,
        use: [
          'style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.m?js$/,
        exclude: /(node_modules|bower_components)/,
        resolve: {
            extensions: [".js",".json"]
        },
        use: [{
            loader: 'babel-loader',
            options: {
                presets: [
                    '@babel/preset-env'                   
                 ],
                 plugins: [
                    '@babel/plugin-transform-runtime'
                 ]
            }
        }]
      }
    ],
    exprContextRegExp: /$^/,
    exprContextCritical: false
  }
};

Error that i am getting :

> Warning Failed to make bytecode node8-x64 for file C:\snapshot\server\app\modules\v1\user\routes\routes.js
C:\snapshot\server\app\modules\v1\user\routes\admin-routes.js:1
(function (exports, require, module, __filename, __dirname) { import {
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at Socket.<anonymous> ([eval]:18:19)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at Pipe.onread (net.js:597:20)
C:\snapshot\pickcel-server\app\modules\v1\user\routes\admin-routes.js:1
(function (exports, require, module, __filename, __dirname) { import {
                                                              ^^^^^^

What will be the reason ??

Can someone give some suggestion or strategy ?

Regards

uds
  • 9
  • 3
  • Are you running `pkg . --debug` from inside the `output` directory? That's where the transpiled code is saved. – robertklep Oct 08 '18 at 08:22
  • May be duplicate of https://stackoverflow.com/questions/37634198/node-error-syntaxerror-unexpected-token-import – Milad Aghamohammadi Oct 08 '18 at 08:37
  • @robertklep no, i am running "pkg . --debug" from root directory of project. – uds Oct 08 '18 at 10:04
  • @uds `pkg .` will use the `bin` property of `package.json` (in your case `app.js`) and use that as entry file. I assume that that file isn't transpiled. Try `pkg build/output/main.bundle.js` (or `pkg output/main.bundle.js`, not sure exactly where your bundle file lives) – robertklep Oct 08 '18 at 10:40
  • @robertklep Thanks dude. I just changed the entry to bundled file. and it worked like charm. – uds Oct 08 '18 at 15:24

1 Answers1

1

pkg . will use the bin property of package.json and use that to determine the entry file, which in your case is app.js.

Assuming that that file isn't transpiled, and you want to package the transpiled bundle, try this:

pkg build/output/main.bundle.js --debug
robertklep
  • 198,204
  • 35
  • 394
  • 381