简体   繁体   中英

NodeJS: SyntaxError: Unexpected token 'export' when trying to create a node module package

Firstly, I am very to the world or Node.js etc so apologies if this is an obvious problem.

I am creating a custom packages with a few components:

One component for example is this?

class MailProcess {
    constructor(name, code) {
        this.name = name;
        this.code = code;
    }
}

module.exports = {MailProcess: MailProcess};

and in my index file:

export {MailProcess} from "./mail/MailProcess";
// export more components like above

and my json file:

{
  "name": "packagename",
  "version": "1.0.3",
  "description": "Private package",
  "main": "index.js",
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/...."
  },
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "aws-sdk": "^2.1172.0",
    "mysql": "^2.18.1"
  }
}

and in my Main code I (after installing the above package) I implement like this:

let {MailProcess} = require('@myusername/mypackagename');

but the error get thrown of

SyntaxError: Unexpected token 'export' relating to the index file.

I am wondering what I might be doing wrong. Essentially my pack contains a few different classes and I try to export them in index so the code using this package has access

Thanks for any help

Ok so I had this same issue, albeit with a different setup and I stumbled upon this question. At this point it may not be needed anymore, but I wanted to share what solved the problem in my case, in case it may help somebody else down the line.

My setup was basically a NodeJs (v16) project acting as a server, a frontend (not relevant to the problem) and the shared Types package.

My issue was that while everything worked fine when using npm link , it would not when publishing the Types package and installing it. Needless to say that the error was the same one:

export * from "./blocks";
^^^^^^

SyntaxError: Unexpected token 'export'

More specifically, in the top level index.ts of the Types package, which was "exporting everything out".

To give an idea, this was the relevant part of my package.json for my Types package:

"name": "vnotes-types",
  "version": "1.1.3",
  "description": "types for the VNotes application",
  "main": "./src/index.ts",
  "scripts": {
    "compile": "tsc"
  },

Moreover, on tsc I was compiling everything to a ./lib folder.

And the solution, basically, was:

  • Changing "main": "./src/index.ts" to "main": "./lib/index.js", . Here the problem was that I would employ the non-compiled index.ts ( /src/index.ts ) and import everything from there.
  • Adding "types": "./lib/index.d.ts" . Basically points out to the type declarations.
  • I think this one isn't 100% necessary, but it reduces the size and clutter of the package: "files": ["./lib"] will only bundle the /lib folder, so your /src folder won't be included in the installation.

Hope this one helps people avoid wasting a stupid amount of time like I did to solve this issue.

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