New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

babel/babel-node won't transpile node_modules #8802

Open
Billy- opened this Issue Oct 2, 2018 · 8 comments

Comments

Projects
None yet
5 participants
@Billy-

Billy- commented Oct 2, 2018

Possible Bug Report / Question

I know this has been covered in a few different places, but they all seem to use webpack or something else, and none of the solutions worked for me. I am just using babel - babel-cli to build and babel-node in conjunction with nodemon for development.

I have some namespaced node modules, which distribute their (untranspiled) source code. I want this folder to be transpiled, but nothing I have tried has worked. Notice below the console.log in the ignore function. This is never called with any files in node_modules.

Based on this comment I added exclude: [] and include: [/src/, /node_modules/], but neither made any difference.

Babel Configuration (.babelrc, package.json, cli command)

Directory structure

/my-project
  /node_modules
  /src
    ...files
  package.json
  yarn.lock
  babel.config.js

package.json

The relevant scripts/commands are start-dev and babel

{
  "name": "firmware-service",
  "version": "1.0.0",
  "main": "index.js",
  "license": "Proprietary",
  "private": true,
  "repository": {
    "type": "git",
    "url": "git@private-repo/firmware-service.git"
  },
  "scripts": {
    "start": "node dist/app.js",
    "start-dev": "nodemon src/app.js --exec \"babel-node\"",
    "dev": "docker-compose up --build",
    "unit-test": "docker-compose -p firmware-service-unit-tests -f docker-compose.unit-test.yml up --exit-code-from unit-tests --build",
    "integration-test": "docker-compose -p firmware-service-integ-tests -f docker-compose.integration-test.yml up --exit-code-from integration-tests --build",
    "test": "docker-compose -p firmware-service-tests -f docker-compose.test.yml up --exit-code-from tests --build",
    "babel": "babel src -d ./dist",
    "lint": "eslint ./src"
  },
  "dependencies": {
    "@my-namespace/auth": "^2.2.0",
    "@my-namespace/server-logger": "^3.1.1",
    "@my-namespace/utilities": "^1.4.1",
    "axios": "^0.18.0",
    "express": "^4.16.3",
    "helmet": "^3.12.1",
    "jsonwebtoken": "^8.2.2",
    "mongoose": "^5.1.3",
    "multer": "^1.3.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.1.2",
    "@babel/core": "^7.1.2",
    "@babel/node": "^7.0.0",
    "@babel/preset-env": "^7.1.0",
    "eslint": "^4.19.1",
    "eslint-config-standard": "^11.0.0",
    "eslint-plugin-import": "^2.12.0",
    "eslint-plugin-node": "^6.0.1",
    "eslint-plugin-promise": "^3.8.0",
    "eslint-plugin-standard": "^3.1.0",
    "expect": "^23.5.0",
    "mocha": "^5.2.0",
    "nodemon": "^1.17.5",
    "supertest": "^3.1.0"
  }
}

babel.config.js

const config = {
  'presets': [
    [
      '@babel/preset-env',
      {
        'targets': {
          'node': 'current',
        },
      },
    ],
  ],
  'retainLines': true,
  'plugins': [
    // 'lodash',
    // 'transform-runtime',
    // 'syntax-object-rest-spread',
  ],
  include: [/src/, /node_modules/],
  exclude: [],
  ignore: [filename => {
    console.log({ filename })
    if (!/\/node_modules\//.test(filename)) {
      return false // if not in node_modules, we want to compile it
    } else if (/\/node_modules\/@my-namespace\//.test(filename)) {
      // its our source code, so we want to compile it
      return false
    }
    // it's in node modules and NOT our source code
    return true
  }],
}

module.exports = config

Error Message

[nodemon] starting `babel-node src/app.js`                 
{ filename: '/home/bmathews/projects/firmware-service/src/app.js' }                                                    
/home/bmathews/projects/firmware-service/node_modules/@my-namespace/server-logger/src/index.js:1                            
(function (exports, require, module, __filename, __dirname) { export { default, setTransports } from './logger'        
                                                              ^^^^^^                                                   

SyntaxError: Unexpected token export                       

Environment

"@babel/cli": "^7.1.2",
"@babel/core": "^7.1.2",
"@babel/node": "^7.0.0",
"@babel/preset-env": "^7.1.0",
  • Node v10.10.0 / npm v6.4.1 / yarn v1.9.4
  • OS: Ubuntu 18.04.1
  • Monorepo: no
  • How you are using Babel: cli, babel-node
@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Oct 2, 2018

Collaborator

Hey @Billy-! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Oct 2, 2018

Hey @Billy-! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@mlavina

This comment has been minimized.

Show comment
Hide comment
@mlavina

mlavina Oct 4, 2018

Contributor

Hey @Billy-

ignore: [filename => {
    console.log({ filename })
    if (!/\/node_modules\//.test(filename)) {
      return false // if not in node_modules, we want to compile it
    } else if (/\/node_modules\/@my-namespace\//.test(filename)) {
      // its our source code, so we want to compile it
      return false
    }

I think it's because you are adding node_modules to your ignore and that will immediately end all babel processes
https://babeljs.io/docs/en/options#ignore

I imagine that trumps the include you have.

Maybe try removing that and see if that fixes it?

Contributor

mlavina commented Oct 4, 2018

Hey @Billy-

ignore: [filename => {
    console.log({ filename })
    if (!/\/node_modules\//.test(filename)) {
      return false // if not in node_modules, we want to compile it
    } else if (/\/node_modules\/@my-namespace\//.test(filename)) {
      // its our source code, so we want to compile it
      return false
    }

I think it's because you are adding node_modules to your ignore and that will immediately end all babel processes
https://babeljs.io/docs/en/options#ignore

I imagine that trumps the include you have.

Maybe try removing that and see if that fixes it?

@Billy-

This comment has been minimized.

Show comment
Hide comment
@Billy-

Billy- Oct 4, 2018

@mlavina that function should return false if the filename is not in node_modules (false being don't ignore), then false again if it's in node_modules but also in my @my-namespace namespace. After that, it will return true as anything that doesn't match those two conditions must be some other node_module.

Billy- commented Oct 4, 2018

@mlavina that function should return false if the filename is not in node_modules (false being don't ignore), then false again if it's in node_modules but also in my @my-namespace namespace. After that, it will return true as anything that doesn't match those two conditions must be some other node_module.

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Oct 4, 2018

Member

Probably also

ignore: /node_modules\/(?!@my-namespace)/

would work. If you need windows support, replace \/ with [\/\\].

Member

nicolo-ribaudo commented Oct 4, 2018

Probably also

ignore: /node_modules\/(?!@my-namespace)/

would work. If you need windows support, replace \/ with [\/\\].

@Billy-

This comment has been minimized.

Show comment
Hide comment
@Billy-

Billy- Oct 4, 2018

@nicolo-ribaudo unfortunately that isn't making any difference for me :(

I think the issue is before the ignore as the ignore function is never called with a filename in node_modules, only files in src

Billy- commented Oct 4, 2018

@nicolo-ribaudo unfortunately that isn't making any difference for me :(

I think the issue is before the ignore as the ignore function is never called with a filename in node_modules, only files in src

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Oct 4, 2018

Member

Oh it could be because node_modules are excluded by default in Babel 7. Could you try to use that RegExp instead of [] in exclude?

Member

nicolo-ribaudo commented Oct 4, 2018

Oh it could be because node_modules are excluded by default in Babel 7. Could you try to use that RegExp instead of [] in exclude?

@Billy-

This comment has been minimized.

Show comment
Hide comment
@Billy-

Billy- Oct 4, 2018

Oh it could be because node_modules are excluded by default in Babel 7

Yup, that's what I'm failing to override. Adding the RE in exclude also makes no difference. I figured an empty array should be enough to override the default.. But neither work.

Billy- commented Oct 4, 2018

Oh it could be because node_modules are excluded by default in Babel 7

Yup, that's what I'm failing to override. Adding the RE in exclude also makes no difference. I figured an empty array should be enough to override the default.. But neither work.

@nicolo-ribaudo nicolo-ribaudo self-assigned this Oct 19, 2018

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Oct 20, 2018

Member

Primarily the issue is that babel-node overrides automatically unless you pass it the options programmatically. It doesn't care that you set different includes and excludes in your config file. The only way to override the default node_modules auto-ignore would be to pass something like --ignore /some-fake-path in the options.

I don't really view babel-node as something meant for serious use beyond executing random scripts here and there. For any serious use, I'd recommend using node -r ./babel-register foo.js with the ./babel-register.js file loading @babel/register, potentially with ignore: [] passed in those options to override the default ignore behavior.

The docs around this could totally be better, and I agree that reading the config file does seem like a better long-term solution, but it's not what we have now and I'm not sure we could do it without it qualifying as breaking.

Member

loganfsmyth commented Oct 20, 2018

Primarily the issue is that babel-node overrides automatically unless you pass it the options programmatically. It doesn't care that you set different includes and excludes in your config file. The only way to override the default node_modules auto-ignore would be to pass something like --ignore /some-fake-path in the options.

I don't really view babel-node as something meant for serious use beyond executing random scripts here and there. For any serious use, I'd recommend using node -r ./babel-register foo.js with the ./babel-register.js file loading @babel/register, potentially with ignore: [] passed in those options to override the default ignore behavior.

The docs around this could totally be better, and I agree that reading the config file does seem like a better long-term solution, but it's not what we have now and I'm not sure we could do it without it qualifying as breaking.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment