1 关键字

npm

2 问题描述

DevEco版本:3.0.0.993

问题现象:运行npm install @material/material-color-utilities 引入一个第三方npm包,并调用相关内容后,编译报错。

Module not found: Error: Can't resolve 'xxx' in 'xxx'
Did you mean 'xxx.js'?
BREAKING CHANGE: The request 'xx' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

 

3 问题原因

3.1 正常机制

运行npm install @material/material-color-utilities 引入一个第三方npm包后,能够正常编译工程,并正常调用该第三方npm。

3.2 异常机制

运行npm install @material/material-color-utilities 引入一个第三方npm包,并调用相关内容后,编译报错。

Module not found: Error: Can't resolve 'xxx' in 'xxx'
Did you mean 'xxx.js'?
BREAKING CHANGE: The request 'xx' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

如下图:

4 解决方案

将导入的第三方包:@material/material-color-utilities下package.json文件中的"type": "module"配置删除,重新编译构建,可编译成功并正常引用。

 

5 定位过程

通过报错日志中的一段话:"(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '.mjs' file, or a '.js' file where the package.json contains '"type": "module"').",该段意思是在pachkage.json中强制设置了EcmaScript模块,是由于package.json包含'"type":"module"',导致node在编译时无法编译。

因此尝试将package.json中的"type":"module"删除,进行验证,发现原因就在于此,于是问题修复。

6 知识分享

在导入第三方npm后,若出现编译问题,可检查旗下的package.json文件的配置问题。

 

 

 

Logo

社区规范:仅讨论OpenHarmony相关问题。

更多推荐