我创建自己的npm包并发布它但是当我尝试使用它时,它给了我一个错误

时间:2018-04-16 17:19:55

标签: reactjs npm package npm-install

我创建了reactjs组件并使其在npm上发布。我在我的另一个项目中安装它,现在当我尝试导入它时给出完整的路径,如

import RippleIcon from '../node_modules/rippleicon/src/RippleIcon';

它给我一个错误

./node_modules/rippleicon/src/RippleIcon.js
Module parse failed: Unexpected token (10:8)
You may need an appropriate loader to handle this file type.
|   render() {
|     return (
|         <i className={this.props.icon+" RippleIcon"}/>
|     );
|   }

rippleicon是我的npm包名,我在create-react-app上创建了我的包。欢迎任何建议。谢谢。

这是我的package.json

{
 "name": "packagetesting",
 "version": "0.1.0",
 "private": true,
 "dependencies": {
   "react": "^16.3.2",
   "react-dom": "^16.3.2",
   "react-scripts": "1.1.4",
   "rippleicon": "^0.1.1"
},
"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test --env=jsdom",
  "eject": "react-scripts eject"
}
}

1 个答案:

答案 0 :(得分:1)

创建节点包并将其链接到项目相对容易。假设您在同一路径上有mypackagemyproject,请尝试以下步骤:

1.-套餐

$ mkdir mypackage
$ cd mypackage/
$ npm init -y
$ echo "exports.myfunc = function() { console.log(\"Hello\"); }" > index.js
$ cd ..

package.json内容将为:

{
  "name": "mypackage",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

2.-项目

$ mkdir myproject
$ cd myproject/
$ npm init -y
$ npm install "file:../mypackage" --save
$ echo "var mp = require('mypackage'); mp.myfunc();" > index.js
$ node index.js

您将看到打印出“Hello”。其package.json内容将为:

{
  "name": "myproject",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "mypackage": "file:../mypackage"
  }
}