简体   繁体   中英

How to transpile a single node package when using @babel/register in node js?

I'm running a node server for SSR purposes. To be able to import my (ES6+) React components I require('@babel/register') at the top of my file, however, some of these components have dependencies on a node_modules package (OpenLayers) that is in ES6 and isn't transpiled by Babel.

I tried adding an { ignore: [/node_modules\\/(?!ol)/] } but then I start getting errors on somewhere else apparently related to babel:

TypeError: Cannot read property 'from' of undefined
at Converter.toBase64 (<MY_PROJECT>\node_modules\convert-source-map\index.js:61:28)
at Converter.toComment (<MY_PROJECT>\node_modules\convert-source-map\index.js:65:21)
...

EDIT: another option would be to mock this module altogether, since I don't really need the functionality in SSR land.

This is the way I procced without using register:

  1. Package installation (dev)

    npm install @babel/core @babel/node @babel/preset-env --save-dev

  2. Create .babelrc file in the project root, and fill it with this:

    { "presets": [ "@babel/preset-env" ] }

  3. Then in package json file (scripts object), you can add this:

    "dev": "nodemon --exec babel-node ./bin/www",

That is because I use nodemon package, and my main file is www (expressjs generator). And you can run :

npm start dev

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