search

Home  >  Q&A  >  body text

node.js - npm 发布ES6模块后再引入,import出错?

D:\iota\code\web\iota-api-server-scaffold\node_modules\iota-user\index.js:4
import * as auth from './src/auth';
^^^^^^
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Module._extensions..js (module.js:550:10)
    at Object.require.extensions.(anonymous function) [as .js] (C:\Users\35824\AppData\Roaming\npm\node_modules\babel-cli\node_modules\babel-register\lib\node.js:166:7)
    at Module.load (module.js:458:32)
    at tryModuleLoad (module.js:417:12)
    at Function.Module._load (module.js:409:3)
    at Module.require (module.js:468:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (config.js:10:1)

npm set registry 本地仓库
npm adduser --registry 本地仓库
npm publish

编译用的 babel,
我怀疑是在引入后不识别模块里的node_modules了,
那么是我发布的方式不对,还是需要什么命令转换?

伊谢尔伦伊谢尔伦2784 days ago630

reply all(3)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 14:03:12

    1. Does "babel for compilation" refer to the class library project iota-user, or the project currently referencing the class library iota-api-server-scaffold?

    2. What file does the
    3. field in

      of the iota-userpackage.jsonmain project refer to? babelAfter compilation, or the source file before the compiler?

    It is recommended that you read package.json completely first, and then I will give you a reference project: angular-baidu-map. My source code is written in es6, compiled through webpack + babel, and the compiled file is published in npm. The main field points to dist/angular-baidu-map.js (the compiled file).

    When others are also using babel or other compilation environments that support the ES6 module specification, they can use import {ngBaiduMap} from 'angular-baidu-map'; to reference my module

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:03:12

    {
        test: /\.js$/,
        loader: 'babel',
        exclude: /node_modules/
    }

    This is the usual babel配置. I found that it was not there. I filtered the node_modules folder. That is to say, the dependencies installed by the npm method will not be compiled by Babel... and import is not compiled. It cannot be executed directly...

    It would be best if the release on npm can be compiled and then released. If it is someone else's project and there is no way to change it, then npm has been downloaded and moved to the project folder and then referenced.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 14:03:12

    babel or other converted modules see @leftstick’s answer

    Native support is a complex topic, please read this article first: https://medium.com/@nodesource/es-modules-and-node-js-hard-choices-2b6995e4d491#.j7lkau8kq

    reply
    0
  • Cancelreply