如何使用npm发布的库?

时间:2019-05-07 12:00:46

标签: javascript npm npm-publish

我已经制作并发布到NPM,这是我自己的库,我想使用它。这是几个可以协同工作的类。 但是我做不到。 我应该怎么用?

我的图书馆的main.js

'use strict';

import RogerClock from './src/RogerClock';
import RogerSprite from './src/RogerSprite';
import RogerRegularSheet from './src/RogerRegularSheet';
import RogerSheet from './src/RogerSheet';
import RogerAnimation from './src/RogerAnimation';
import RogerObject from './src/RogerObject';

module.exports = {
    clock: RogerClock,
    sprite: RogerSprite,
    sheet: RogerSheet,
    regularSheet: RogerRegularSheet,
    animation: RogerAnimation,
    object: RogerObject
};

我在项目中使用它的方式:

'use strict';

const Roger = require("@pabrick/roger.js");
const rClock = new Roger.Clock(0.1);
rClock.start();

使用此webpack.config:

var path = require('path');
var webpack = require('webpack');
const Uglify = require("uglifyjs-webpack-plugin");

module.exports = {
    devtool: 'source-map',
    entry: {
        js: './src/main.js'
    },
    output: {
        filename: 'main.js',
        path: path.resolve(__dirname, 'dist'),
        pathinfo: true,
        sourceMapFilename: "./main.js.map",
        devtoolLineToLine: true
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/,
                query: {
                    presets: ['env']
                }
            }
        ]
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin()
    ],
    stats: {
        colors: true
    }
};

我希望可以工作,但是webpack会给我这个错误:

ERROR in ./src/main.js
Module build failed: TypeError: Cannot read property 'bindings' of null
    at Scope.moveBindingTo (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/scope/index.js:864:13)
    at BlockScoping.updateScopeInfo (/home/orbituser/projects/personal/test/node_modules/babel-plugin-transform-es2015-block-scoping/lib/index.js:364:17)
    at BlockScoping.run (/home/orbituser/projects/personal/test/node_modules/babel-plugin-transform-es2015-block-scoping/lib/index.js:330:12)
    at PluginPass.BlockStatementSwitchStatementProgram (/home/orbituser/projects/personal/test/node_modules/babel-plugin-transform-es2015-block-scoping/lib/index.js:70:24)
    at newFn (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/visitors.js:193:21)
    at NodePath._call (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/path/context.js:53:20)
    at NodePath.call (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/path/context.js:40:17)
    at NodePath.visit (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/path/context.js:88:12)
    at TraversalContext.visitQueue (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/context.js:118:16)
    at TraversalContext.visitSingle (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/context.js:90:19)
    at TraversalContext.visit (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/context.js:146:19)
    at Function.traverse.node (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/index.js:94:17)
    at traverse (/home/orbituser/projects/personal/test/node_modules/@babel/traverse/lib/index.js:76:12)
    at transformFile (/home/orbituser/projects/personal/test/node_modules/@babel/core/lib/transformation/index.js:88:29)
    at runSync (/home/orbituser/projects/personal/test/node_modules/@babel/core/lib/transformation/index.js:45:3)
    at runAsync (/home/orbituser/projects/personal/test/node_modules/@babel/core/lib/transformation/index.js:35:14)
    at process.nextTick (/home/orbituser/projects/personal/test/node_modules/@babel/core/lib/transform.js:34:34)
    at _combinedTickCallback (internal/process/next_tick.js:132:7)
    at process._tickCallback (internal/process/next_tick.js:181:9)

0 个答案:

没有答案