Skip to content
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

import npm 模块 报错 #342

Closed
xutm opened this issue Aug 22, 2017 · 9 comments
Closed

import npm 模块 报错 #342

xutm opened this issue Aug 22, 2017 · 9 comments
Labels
inactive Inactive issue, will be auto closed in 7 days

Comments

@xutm
Copy link

xutm commented Aug 22, 2017

引用npm模块fecha报错

index.js:1 Uncaught SyntaxError: Unexpected token export factory.js:1 Uncaught SyntaxError: Unexpected token import

import fecha from 'fecha'

引用自己定义的npm模块报错

WAService.js:9 Uncaught Error: module "npm/@xhs/http/index.js" is not definedrequire @ WAService.js:9(anonymous function) @ WAService.js:9(anonymous function) @ app.js [sm]:5require @ WAService.js:9(anonymous function) @ app.js:130 base.js:137 Uncaught TypeError: Cannot read property '$pages' of undefined

import http from '@xhs/http'

在线等待回复

@Gcaufy
Copy link
Collaborator

Gcaufy commented Aug 22, 2017

这是两个错误?没太看懂你的描述

第一个好像是没有编译的问题,dist里的代码不可能有import
第二个你的看看dist/npm中有没有对应的文件,然后看看require路径对不对。

@xutm
Copy link
Author

xutm commented Aug 22, 2017

  • 第一个问题,是在代码里面import fecha from ‘fecha’,然后运行wepy build --watch 微信开发者工具中报的错
  • 第二个问题,我在代码里面import http from '@xhs/http',然后运行wepy build --watch,微信开发者工具中报的错误

两个问题之间的区别在于,第一个npm包是开源的fecha,第二个是我自己构建的npm包

@Gcaufy
Copy link
Collaborator

Gcaufy commented Aug 22, 2017

编译完后都不会有 import关键字的。。怎么会报Unexpected token import
你看一下编译完后的代码先。看看是因为没有编译还是弄错了目录文件之类的。

@xutm
Copy link
Author

xutm commented Aug 22, 2017

第二个问题的可能原因

  • wepy build --watch 获取node_modules中的包,无法识别,node_modules对应的包中的 import
export { default as dualColumnCardTransform } from './src/dualColumnCardTransform'
export { default as getItemStockStatus } from './src/getItemStockStatus'
export { default as urlUtils } from './src/url-utils'
export { default as dateUtils, formatNumber, formatter, dateStringToTimestamp } from './src/date-utils'
export { default as priceUtil } from './src/price'
export { default as couponUtil } from './src/coupon'

上面的代码是node_modules中index.js中,所引入的模块,均没有被解析

@xutm
Copy link
Author

xutm commented Aug 22, 2017

@Gcaufy 第二个问题,能否重现?

@xutm
Copy link
Author

xutm commented Aug 25, 2017

看了下源码,确实只支持但文件引入

getPkgConfig (lib) {
        let pkg = util.readFile(path.join(modulesPath, lib, 'package.json'));
        try {
            pkg = JSON.parse(pkg);
        } catch (e) {
            pkg = null;
        }
        return pkg;
    },`
` let pkg = this.getPkgConfig(lib);
                if (!pkg) {
                    throw Error('找不到模块: ' + lib);
                }
                let main = pkg.main || 'index.js';
                if (pkg.browser && typeof pkg.browser === 'string') {
                    main = pkg.browser;
                }
                source = path.join(modulesPath, lib, main);
                target = path.join(npmPath, lib, main);
                lib += path.sep + main;
                ext = '';
                needCopy = true;

@Gcaufy 给我一个回复?这个问题你们后续会解决吗?还是。。。

@Gcaufy
Copy link
Collaborator

Gcaufy commented Aug 28, 2017

依然不太明白问题,
是wepy的问题,后续肯定会修复的。
介意发份简单的DEMO代码给我重现一下。

@wulunyi
Copy link

wulunyi commented Aug 29, 2017

貌似跟我的引用私有 npm 包一样
下面是我的解决可试一下
这个问题我找到了问题的地点:
对于带 scopes 的私有组件——像我自己的 @ jt/jt-wepy-h-video 组件;
通过 npm 安装的引用写法为 import Hvideo from “@ jt/jt-wepy-h-video”;

问题出现在以下两个地方:
1、在 complie-wpy.js 源码 229 行 get imports 时使用正则表达式 /import\s*([\w-_])\sfrom\s*'"['"]/ig 无法匹配带有“@”符号的情况;

我的调整::/import\s*([\w-_])\sfrom\s*['"]([\w-_./\ @]*)['"]/ig;

2、在 util.js 中 findComponent 方法中 124 行使用了 com.indexOf(path.sep) !== -1 进行判断是否是 node_modules 中的模块,忽略了 私有库中带有"@"符号这种情况,导致找不到这个组件;

我的调整:添加 com.indexOf('@') === -1 判断条件来命中带"@"符号这种情况;

我这是找原文件中的位置,你可要找编译后的位置调整下就OK

@stale
Copy link

stale bot commented Jun 13, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
因为这个 Issue 最近没有任何有效回复,所以被自动标记为了stale。 如果在未来7天依旧没有任何激活操作,那么该 Issue 将会被自动关闭。 感谢您的提问。

@stale stale bot added the inactive Inactive issue, will be auto closed in 7 days label Jun 13, 2018
@stale stale bot closed this as completed Jun 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
inactive Inactive issue, will be auto closed in 7 days
Projects
None yet
Development

No branches or pull requests

3 participants