Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

vue.js - Vue CLI project fails to import CJS module

I'm trying to use an NPM package in a Vue CLI project (Vue CLI 4 / Webpack 4) but Webpack is failing to import the module in a way I've never seen before.

The package ("scino") is a CommonJS module that has one dependency: decimal.js v7 (which appears to ship a CJS as well as an MJS version, for what it's worth).

When I try to use it in a project, I get the error:

TypeError: Decimal is not a constructor

That looks like it isn't importing the default export from the scino package's dependency correctly. I've tried the ESM import syntax, as well as variations on the CJS require syntax:

import scino from 'scino';
// or
const scino = require('scino');
// or 
const scino = require('scino').default;

I thought using the require syntax would tell Webpack to use CJS-style module imports, but neither seems to work.

Interestingly, using the package in a codepen works just fine... so I'm pretty sure it has to do with how Webpack is importing scino (and its decimal.js dependency).

How can I get webpack to import this package correctly?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...