我在从节点的hash
包中导入crypto
时遇到问题。这是我得到的错误:
错误
[~/Projects/rollup] yarn run build
yarn run v1.2.1
$ rollup -c
src/main.js → ./build/app.js...
(!) Unresolved dependencies
https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency
crypto (imported by src/main.js)
(!) Missing global variable name
Use options.globals to specify browser global variable names corresponding to external modules
crypto (guessing 'crypto')
created ./build/app.js in 791ms
✨ Done in 4.61s.
的src / main.js
import { hash } from 'crypto';
hash.digest('hex');
rollup.config.js
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
export default {
input: 'src/main.js',
output: {
file: './build/app.js',
format: 'iife'
},
plugins: [
resolve({
jsnext: true,
main: true,
browser: true,
}),
commonjs(),
babel({
exclude: 'node_modules/**'
})
]
};
的package.json
{
"name": "rollup",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"babel-core": "^6.26.0",
"babel-preset-env": "^1.6.1",
"lodash-es": "^4.17.4",
"rollup": "^0.50.0",
"rollup-plugin-babel": "^3.0.2",
"rollup-plugin-commonjs": "^8.2.5",
"rollup-plugin-node-resolve": "^3.0.0"
},
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w"
}
}
奇怪的是,如果我尝试从template
导入lodash-es
,我不会收到任何错误:
import template from 'lodash-es';
console.log('test');
我是否只能安装适用于浏览器的软件包?
答案 0 :(得分:0)
使用节点模块时,您必须使用rollup-plugin-node-builtins
。我将以下内容添加到rollup.config.js
文件中:
import babel from 'rollup-plugin-babel';
import commonjs from 'rollup-plugin-commonjs';
import resolve from 'rollup-plugin-node-resolve';
import builtins from 'rollup-plugin-node-builtins';
export default {
input: 'src/main.js',
output: {
file: './build/app.js',
format: 'iife'
},
plugins: [
builtins(),
resolve({
jsnext: true,
main: true,
browser: true,
}),
commonjs(),
babel({
exclude: 'node_modules/**'
})
]
};