Babel-node cannot compile js under node_modules, how to resolve it?

180 views Asked by At

I've struggled for this question for a long time, i've tried all possible solutions by google, but it still didn't work.
Here, i set an brief example to describe the problem. Assume we created the structure:

test
|  
|____ test1.js
|
|____ node_modules
|     |
|     |____ test2.js
|     |
|     |____ [babel modules...]
|  
|____ .babelrc
|  
|____ package.json

test1.js / test2.js:

console.log({
  ...{a: 123}
})

.babelrc:

{
  "presets": ["es2015", "stage-2"]
}

Test in normal situation

node_modules/.bin/babel-node test1.js

Result:

{ a: 123 }

Test in node_modules situation

node_modules/.bin/babel-node node_modules/test2.js

Result:

...{a: 123}
^^^

SyntaxError: Unexpected token ...
1

There are 1 answers

1
Isidrok On

Seems it is not using/finding the presets anymore, try copying .babelrc to node_modules.