如何使节点项目可执行?

时间:2018-10-08 08:02:23

标签: node.js webpack babeljs zeit-pkg

我在node.js和express.js中有我的项目。我需要制作一个node.js项目的可执行文件。

项目配置:

使用ES6功能:导入和动态导入,为此,我正在使用babel.js。

项目状态:

项目正常运行没问题。

该项目的移植正在进行中。捆绑创建就可以了。

问题/错误/问题:

使用命令打包时:

  

pkg。 -调试

给出“导入”错误

package.json配置:

{
  "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
  }
};

我得到的错误:

> 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 {
                                                              ^^^^^^

原因是什么?

有人可以提出一些建议或策略吗?

致谢

1 个答案:

答案 0 :(得分:1)

pkg .将使用bin的{​​{1}}属性,并使用该属性来确定条目文件,在您的情况下为package.json

假设该文件没有被转译,并且您想打包已转译的捆绑软件,请尝试以下操作:

app.js